Skip to content

Commit 4b41af3

Browse files
committed
Dismissable admin notice
1 parent 8c4b4c4 commit 4b41af3

File tree

3 files changed

+265
-45
lines changed

3 files changed

+265
-45
lines changed

plugins/wp-graphql-headless-webhooks/src/Admin/WebhooksAdmin.php

Lines changed: 122 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -317,25 +317,59 @@ public function handle_test_webhook(): void {
317317
}
318318
}
319319

320+
// Log the test request for debugging
321+
error_log( sprintf(
322+
'[Webhook Test] Sending %s request to %s with payload: %s',
323+
$args['method'],
324+
$url,
325+
wp_json_encode( $payload )
326+
) );
327+
320328
$response = wp_remote_request( $url, $args );
321329

322330
if ( is_wp_error( $response ) ) {
323-
wp_send_json_error( $response->get_error_message() );
331+
wp_send_json_error( array(
332+
'message' => sprintf(
333+
__( 'Connection failed: %s', 'wp-graphql-headless-webhooks' ),
334+
$response->get_error_message()
335+
),
336+
'error_code' => $response->get_error_code(),
337+
'error_data' => $response->get_error_data(),
338+
) );
324339
}
325340

326341
// Get response details
327342
$response_code = wp_remote_retrieve_response_code( $response );
328343
$response_body = wp_remote_retrieve_body( $response );
344+
$response_headers = wp_remote_retrieve_headers( $response );
345+
346+
// Try to parse JSON response
347+
$parsed_body = json_decode( $response_body, true );
348+
if ( json_last_error() === JSON_ERROR_NONE ) {
349+
$response_body_display = wp_json_encode( $parsed_body, JSON_PRETTY_PRINT );
350+
} else {
351+
// Strip HTML tags and decode entities from response body
352+
$response_body = wp_strip_all_tags( $response_body );
353+
$response_body = html_entity_decode( $response_body, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
354+
$response_body_display = $response_body;
355+
}
356+
357+
// Determine success based on response code
358+
$is_success = $response_code >= 200 && $response_code < 300;
329359

330-
// Strip HTML tags and decode entities from response body
331-
$response_body = wp_strip_all_tags( $response_body );
332-
$response_body = html_entity_decode( $response_body, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
360+
// Build detailed response message
361+
$message = $is_success
362+
? __( 'Webhook test completed successfully!', 'wp-graphql-headless-webhooks' )
363+
: sprintf( __( 'Webhook test failed with status %d', 'wp-graphql-headless-webhooks' ), $response_code );
333364

334365
// Send structured response data
335366
wp_send_json_success( array(
336-
'message' => __( 'Webhook sent successfully!', 'wp-graphql-headless-webhooks' ),
367+
'message' => $message,
368+
'success' => $is_success,
337369
'response_code' => $response_code,
338-
'response_body' => substr( $response_body, 0, 200 ) // Limit response body to 200 chars
370+
'response_body' => substr( $response_body_display, 0, 500 ), // Limit response body to 500 chars
371+
'response_headers' => $response_headers->getAll(),
372+
'test_payload' => $payload, // Include what was sent for debugging
339373
) );
340374
}
341375

@@ -378,26 +412,95 @@ private function get_test_payload_for_event( string $event ): array {
378412
);
379413
break;
380414

381-
case 'post.published':
382-
case 'post.updated':
383-
case 'post.deleted':
384-
$base_payload['data'] = array(
385-
'id' => 999999,
386-
'title' => 'Test Post (Not Real)',
387-
'status' => 'test',
388-
'author' => 0,
389-
'test_note' => 'This is test data - no actual post exists',
415+
case 'post_published':
416+
case 'post_updated':
417+
case 'post_deleted':
418+
// Match the actual webhook payload structure
419+
$test_post_id = 999999;
420+
$base_payload = array(
421+
'post_id' => $test_post_id,
422+
'post' => array(
423+
'id' => $test_post_id,
424+
'title' => 'Test Post - Hello World',
425+
'slug' => 'test-post-hello-world',
426+
'uri' => '/test-post-hello-world/',
427+
'status' => 'publish',
428+
'type' => 'post',
429+
'date' => current_time( 'mysql' ),
430+
'modified' => current_time( 'mysql' ),
431+
),
432+
'path' => '/test-post-hello-world/',
433+
'test' => true,
434+
'test_mode' => true,
435+
'message' => 'This is a TEST webhook payload - no actual post was affected',
390436
);
391437
break;
392438

393-
case 'user.created':
439+
case 'post_meta_change':
440+
$base_payload['post_id'] = 999999;
441+
$base_payload['meta_key'] = 'test_meta_key';
442+
$base_payload['test_note'] = 'This is test data - no actual meta was changed';
443+
break;
444+
445+
case 'term_created':
446+
case 'term_assigned':
447+
case 'term_unassigned':
448+
case 'term_deleted':
449+
$base_payload['term_id'] = 999999;
450+
$base_payload['taxonomy'] = 'category';
451+
if ( $event === 'term_assigned' || $event === 'term_unassigned' ) {
452+
$base_payload['object_id'] = 888888;
453+
}
454+
$base_payload['test_note'] = 'This is test data - no actual term was affected';
455+
break;
456+
457+
case 'user_created':
458+
case 'user_deleted':
459+
$base_payload['user_id'] = 999999;
394460
$base_payload['data'] = array(
395-
'id' => 999999,
396461
'username' => 'test_webhook_user',
397462
'email' => '[email protected]',
398-
'role' => 'test',
399-
'test_note' => 'This is test data - no actual user exists',
463+
'role' => 'subscriber',
400464
);
465+
$base_payload['test_note'] = 'This is test data - no actual user was affected';
466+
break;
467+
468+
case 'user_assigned':
469+
case 'user_reassigned':
470+
$base_payload['post_id'] = 999999;
471+
$base_payload['author_id'] = 888888;
472+
if ( $event === 'user_reassigned' ) {
473+
$base_payload['old_author_id'] = 777777;
474+
$base_payload['new_author_id'] = 888888;
475+
}
476+
$base_payload['test_note'] = 'This is test data - no actual assignment was made';
477+
break;
478+
479+
case 'media_uploaded':
480+
case 'media_updated':
481+
case 'media_deleted':
482+
$base_payload['post_id'] = 999999;
483+
$base_payload['post'] = array(
484+
'id' => 999999,
485+
'title' => 'Test Media File',
486+
'slug' => 'test-media-file',
487+
'uri' => '/test-media-file/',
488+
'status' => 'inherit',
489+
'type' => 'attachment',
490+
'date' => current_time( 'mysql' ),
491+
'modified' => current_time( 'mysql' ),
492+
);
493+
$base_payload['path'] = '/test-media-file/';
494+
$base_payload['test_note'] = 'This is test data - no actual media was affected';
495+
break;
496+
497+
case 'comment_inserted':
498+
case 'comment_status':
499+
$base_payload['comment_id'] = 999999;
500+
if ( $event === 'comment_status' ) {
501+
$base_payload['new_status'] = 'approved';
502+
}
503+
$base_payload['test_note'] = 'This is test data - no actual comment was affected';
401504
break;
402505

403506
default:

plugins/wp-graphql-headless-webhooks/src/Admin/assets/admin.css

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,30 +164,95 @@
164164
/* Test webhook link */
165165
.test-webhook {
166166
cursor: pointer;
167+
text-decoration: none;
167168
}
168169

169170
/* Test webhook button states */
170171
.test-webhook.testing {
171-
color: #3858e9;
172+
color: #666;
172173
cursor: not-allowed;
173174
pointer-events: none;
174175
}
175176

176177
.test-webhook.success {
177-
color: #00a32a;
178+
color: #46b450;
178179
}
179180

180181
.test-webhook.error {
181-
color: #d63638;
182+
color: #dc3232;
183+
}
184+
185+
/* Test webhook result display */
186+
.webhook-test-result td {
187+
padding: 0 !important;
188+
background: transparent !important;
189+
}
190+
191+
.webhook-test-result .notice {
192+
margin: 10px 0;
193+
border-left-width: 4px;
182194
}
183195

184-
/* Webhook test response styling */
185196
.webhook-test-details {
197+
padding: 5px 0;
186198
margin: 0;
187199
}
188200

189201
.webhook-test-details p {
190-
margin: 0.5em 0;
202+
margin: 5px 0;
203+
}
204+
205+
.webhook-test-details .status-success {
206+
color: #46b450;
207+
font-weight: bold;
208+
}
209+
210+
.webhook-test-details .status-error {
211+
color: #dc3232;
212+
font-weight: bold;
213+
}
214+
215+
.webhook-test-details details {
216+
margin: 10px 0;
217+
border: 1px solid #ddd;
218+
border-radius: 3px;
219+
padding: 5px;
220+
background: #f9f9f9;
221+
}
222+
223+
.webhook-test-details summary {
224+
cursor: pointer;
225+
font-weight: 600;
226+
padding: 5px;
227+
user-select: none;
228+
}
229+
230+
.webhook-test-details summary:hover {
231+
background: #f1f1f1;
232+
}
233+
234+
.webhook-test-details pre {
235+
margin: 10px 0 5px;
236+
padding: 10px;
237+
background: #fff;
238+
border: 1px solid #e1e1e1;
239+
border-radius: 3px;
240+
overflow-x: auto;
241+
font-size: 12px;
242+
line-height: 1.4;
243+
white-space: pre-wrap;
244+
word-wrap: break-word;
245+
}
246+
247+
.webhook-test-details pre.webhook-test-payload {
248+
max-height: 300px;
249+
overflow-y: auto;
250+
}
251+
252+
.webhook-test-details pre.webhook-response-body {
253+
background: #f7f7f7;
254+
max-height: 200px;
255+
overflow-y: auto;
191256
}
192257

193258
.webhook-test-details .notice-message-body {

0 commit comments

Comments
 (0)