Skip to content

Commit e0d2f3a

Browse files
committed
fix: webhook handlers trigger correctly and with payloads
1 parent ea70bdd commit e0d2f3a

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

plugins/wp-graphql-headless-webhooks/src/Events/WebhookEventManager.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ public function register_hooks(): void {
5757
*/
5858
private function trigger_webhooks( string $event, array $payload ): void {
5959
$allowed_events = $this->repository->get_allowed_events();
60-
61-
if ( ! in_array( $event, $allowed_events, true ) ) {
60+
if ( ! array_key_exists( $event, $allowed_events ) ) {
61+
error_log( 'Event ' . $event . ' is not allowed. Allowed events: ' . implode( ', ', $allowed_events ) );
6262
return;
6363
}
6464

6565
do_action( 'graphql_webhooks_before_trigger', $event, $payload );
66-
6766
foreach ( $this->repository->get_all() as $webhook ) {
6867
if ( $webhook->event === $event ) {
6968
$this->handler->handle( $webhook, $payload );

plugins/wp-graphql-headless-webhooks/src/Handlers/WebhookHandler.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,34 @@
1111
*/
1212
class WebhookHandler implements Handler {
1313

14-
/**
15-
* Handle the event payload for a specific webhook.
16-
*
17-
* @param Webhook $webhook The Webhook entity instance.
18-
* @param array $payload The event payload data.
19-
*
20-
* @return void
21-
*/
22-
public function handle( Webhook $webhook, array $payload ): void {
23-
$args = [
24-
'headers' => $webhook->headers ?: [ 'Content-Type' => 'application/json' ],
25-
'timeout' => 5,
26-
'blocking' => false,
27-
];
28-
29-
if ( strtoupper( $webhook->method ) === 'GET' ) {
30-
$url = add_query_arg( $payload, $webhook->url );
31-
$args['method'] = 'GET';
32-
} else {
33-
$url = $webhook->url;
34-
$args['method'] = 'POST';
35-
$args['body'] = wp_json_encode( $payload );
36-
if ( empty( $args['headers']['Content-Type'] ) ) {
37-
$args['headers']['Content-Type'] = 'application/json';
38-
}
39-
}
40-
41-
/**
42-
* Filter the payload before sending.
43-
*/
14+
/**
15+
* Handle the event payload for a specific webhook.
16+
*
17+
* @param Webhook $webhook The Webhook entity instance.
18+
* @param array $payload The event payload data.
19+
*
20+
* @return void
21+
*/
22+
public function handle( Webhook $webhook, array $payload ): void {
23+
$args = [
24+
'headers' => $webhook->headers ?: [ 'Content-Type' => 'application/json' ],
25+
'timeout' => 5,
26+
'blocking' => false,
27+
];
4428
$payload = apply_filters( 'graphql_webhooks_payload', $payload, $webhook );
45-
wp_remote_request( $url, $args );
46-
do_action( 'graphql_webhooks_sent', $webhook, $payload );
47-
}
29+
30+
if ( strtoupper( $webhook->method ) === 'GET' ) {
31+
$url = add_query_arg( $payload, $webhook->url );
32+
$args['method'] = 'GET';
33+
} else {
34+
$url = $webhook->url;
35+
$args['method'] = 'POST';
36+
$args['body'] = wp_json_encode( $payload );
37+
if ( empty( $args['headers']['Content-Type'] ) ) {
38+
$args['headers']['Content-Type'] = 'application/json';
39+
}
40+
}
41+
wp_remote_request( $url, $args );
42+
do_action( 'graphql_webhooks_sent', $webhook, $payload );
43+
}
4844
}

plugins/wp-graphql-headless-webhooks/src/Plugin.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ private function includes(): void {
9797
require_once WPGRAPHQL_HEADLESS_WEBHOOKS_PLUGIN_DIR . 'vendor/autoload.php';
9898
}
9999
}
100+
101+
/**
102+
* Get the webhook repository instance.
103+
*
104+
* Provides access to the WebhookRepository for managing webhook data.
105+
*
106+
* @return WebhookRepository The repository instance.
107+
*/
108+
public function get_repository(): WebhookRepository {
109+
return $this->repository;
110+
}
100111

101112
/**
102113
* Prevent cloning.

0 commit comments

Comments
 (0)