7
7
8
8
declare (strict_types=1 );
9
9
10
+ use WPGraphQL \Webhooks \DTO \WebhookDTO ;
10
11
use WPGraphQL \Webhooks \WebhookRegistry ;
12
+ use WPGraphQL \Webhooks \Events \Event ;
11
13
use WPGraphQL \Webhooks \Events \GraphQLEventRegistry ;
14
+ use WPGraphQL \Webhooks \WebhookTypeRegistry ;
12
15
13
16
/**
14
17
* Registers a new webhook type.
27
30
* }
28
31
*/
29
32
if ( ! function_exists ( 'register_webhook_type ' ) ) {
30
- /** @phpstan-ignore missingType.iterableValue */
31
- function register_webhook_type (string $ type , array $ args = []): void {
33
+ function register_webhook_type ( string $ type , array $ args = [] ): void {
32
34
/** @psalm-suppress HookNotFound */
33
35
if ( did_action ( 'graphql_register_webhooks ' ) > 0 ) {
34
- _doing_it_wrong (
35
- 'register_webhook_type ' ,
36
- esc_html__ ( 'Call this before WebhookRegistry::init ' , 'wp-graphql-headless-webhooks ' ),
37
- '0.1.0 '
38
- );
36
+ _doing_it_wrong ( 'register_webhook_type ' , __ ( 'Call this before WebhookRegistry::init ' , 'wp-graphql-headless-webhooks ' ), '0.1.0 ' );
37
+
38
+ return ;
39
39
}
40
+
40
41
/** @psalm-suppress HookNotFound */
41
42
add_action (
42
43
'graphql_register_webhooks ' ,
43
- static function (WebhookRegistry $ webhook_registry ) use ($ type , $ args ): void {
44
- if ( ! isset ( $ args ['events ' ] ) || ! is_array ( $ args ['events ' ] ) ) {
45
- $ args ['events ' ] = [];
46
- }
47
- // Use explicit boolean condition
48
- if (count ($ args ['events ' ]) > 0 ) {
49
- foreach ( $ args ['events ' ] as $ event_type ) {
50
- if ( function_exists ( 'register_graphql_event ' ) ) {
51
- register_graphql_event ( $ event_type );
52
- }
44
+ function (WebhookTypeRegistry $ registry ) use ($ type , $ args ): void {
45
+ $ events = [];
46
+ if ( ! empty ( $ args ['events ' ] ) && is_array ( $ args ['events ' ] ) ) {
47
+ foreach ( $ args ['events ' ] as $ eventData ) {
48
+ $ events [] = new Event (
49
+ $ eventData ['name ' ],
50
+ $ eventData ['hookName ' ],
51
+ $ eventData ['callback ' ] ?? null ,
52
+ $ eventData ['priority ' ] ?? 10 ,
53
+ $ eventData ['argCount ' ] ?? 1
54
+ );
53
55
}
54
56
}
55
- $ webhook_registry ->register_webhook_type ( $ type , $ args );
57
+
58
+ $ webhook = new WebhookDTO (
59
+ $ type ,
60
+ $ args ['label ' ] ?? '' ,
61
+ $ args ['description ' ] ?? '' ,
62
+ $ args ['config ' ] ?? [],
63
+ $ events
64
+ );
65
+
66
+ $ registry ->register_webhook_type ( $ webhook );
56
67
}
57
68
);
58
69
}
@@ -73,7 +84,7 @@ static function (WebhookRegistry $webhook_registry) use ($type, $args): void {
73
84
/**
74
85
* @return \WP_Error|int
75
86
*/
76
- function create_webhook (string $ type , string $ name , array $ config = []) { // @phpstan-ignore missingType.iterableValue
87
+ function create_webhook ( string $ type , string $ name , array $ config = [] ) { // @phpstan-ignore missingType.iterableValue
77
88
return WebhookRegistry::instance ()->create_webhook ( $ type , $ name , $ config );
78
89
}
79
90
@@ -89,7 +100,7 @@ function create_webhook(string $type, string $name, array $config = []) { // @ph
89
100
if ( ! function_exists ( 'get_webhook_type ' ) ) {
90
101
91
102
/** @phpstan-ignore missingType.iterableValue */
92
- function get_webhook_type (string $ type ): ?array {
103
+ function get_webhook_type ( string $ type ): ?array {
93
104
return WebhookRegistry::instance ()->get_webhook_type ( $ type );
94
105
}
95
106
@@ -118,16 +129,24 @@ function get_webhook_types(): array {
118
129
* The registered event will listen to a specified WordPress action (e.g. 'publish_post'),
119
130
* and execute a qualifying callback to potentially dispatch notifications or other side effects.
120
131
*
132
+ * @param Event $event The event object to register.
133
+ *
134
+ * @return void
121
135
*/
122
- function register_graphql_event ( ...$ args ) {
123
- if ( did_action ( 'graphql_register_events ' ) ) {
124
- _doing_it_wrong ( 'register_graphql_event ' , esc_html__ ( 'Call this before EventRegistry::init ' , 'wp-graphql-webhooks ' ), '0.0.1 ' );
136
+ function register_graphql_event ( Event $ event ): void {
137
+ if ( did_action ( 'graphql_register_events ' ) ) {
138
+ _doing_it_wrong (
139
+ __FUNCTION__ ,
140
+ esc_html__ ( 'Call this before EventRegistry::init ' , 'wp-graphql-webhooks ' ),
141
+ '0.0.1 '
142
+ );
143
+ return ;
125
144
}
126
145
127
146
add_action (
128
- 'graphql_register_events ' ,
129
- static function (GraphQLEventRegistry $ event_registry ) use ($ args ) {
130
- $ event_registry ->registerEvent ( ... $ args );
147
+ 'graphql_register_events ' ,
148
+ static function (GraphQLEventRegistry $ event_registry ) use ($ event ) {
149
+ $ event_registry ->register_event ( $ event );
131
150
}
132
151
);
133
152
}
0 commit comments