@@ -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 ' ,
397462398- '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 :
0 commit comments