-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebhookHandler.php
More file actions
44 lines (40 loc) · 1.21 KB
/
Copy pathWebhookHandler.php
File metadata and controls
44 lines (40 loc) · 1.21 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
<?php
namespace WPGraphQL\Webhooks\Handlers;
use WPGraphQL\Webhooks\Entity\Webhook;
use WPGraphQL\Webhooks\Handlers\Interfaces\Handler;
/**
* Class WebhookHandler
*
* Sends the webhook to the configured URL when an event is triggered.
*/
class WebhookHandler implements Handler {
/**
* Handle the event payload for a specific webhook.
*
* @param Webhook $webhook The Webhook entity instance.
* @param array $payload The event payload data.
*
* @return void
*/
public function handle( Webhook $webhook, array $payload ): void {
$args = [
'headers' => $webhook->headers ?: [ 'Content-Type' => 'application/json' ],
'timeout' => 5,
'blocking' => false,
];
$payload = apply_filters( 'graphql_webhooks_payload', $payload, $webhook );
if ( strtoupper( $webhook->method ) === 'GET' ) {
$url = add_query_arg( $payload, $webhook->url );
$args['method'] = 'GET';
} else {
$url = $webhook->url;
$args['method'] = 'POST';
$args['body'] = wp_json_encode( $payload );
if ( empty( $args['headers']['Content-Type'] ) ) {
$args['headers']['Content-Type'] = 'application/json';
}
}
wp_remote_request( $url, $args );
do_action( 'graphql_webhooks_sent', $webhook, $payload );
}
}