1+ <?php
2+
3+ namespace WPGraphQL \Webhooks \Events ;
4+
5+ use WPGraphQL \Webhooks \Events \Interfaces \EventManager ;
6+ use WPGraphQL \Webhooks \Repository \Interfaces \WebhookRepositoryInterface ;
7+ use WPGraphQL \Webhooks \Handlers \Interfaces \Handler ;
8+
9+ /**
10+ * Webhook Event Manager
11+ *
12+ * Manages WordPress events and triggers matching webhooks.
13+ */
14+ class WebhookEventManager implements EventManager {
15+
16+ private WebhookRepositoryInterface $ repository ;
17+ private Handler $ handler ;
18+
19+ /**
20+ * Constructor
21+ *
22+ * @param WebhookRepositoryInterface $repository
23+ * @param Handler $sender
24+ */
25+ public function __construct ( WebhookRepositoryInterface $ repository , $ handler ) {
26+ $ this ->repository = $ repository ;
27+ $ this ->handler = $ handler ;
28+ }
29+
30+ /**
31+ * Register specific WordPress event hooks.
32+ */
33+ public function register_hooks (): void {
34+ add_action ( 'transition_post_status ' , [ $ this , 'on_transition_post_status ' ], 10 , 3 );
35+ add_action ( 'post_updated ' , [ $ this , 'on_post_updated ' ], 10 , 3 );
36+ add_action ( 'deleted_post ' , [ $ this , 'on_deleted_post ' ], 10 , 2 );
37+ add_action ( 'added_post_meta ' , [ $ this , 'on_post_meta_change ' ], 10 , 4 );
38+ add_action ( 'created_term ' , [ $ this , 'on_term_created ' ], 10 , 3 );
39+ add_action ( 'set_object_terms ' , [ $ this , 'on_term_assigned ' ], 10 , 6 );
40+ add_action ( 'delete_term_relationships ' , [ $ this , 'on_term_unassigned ' ], 10 , 3 );
41+ add_action ( 'delete_term ' , [ $ this , 'on_term_deleted ' ], 10 , 4 );
42+ add_action ( 'added_term_meta ' , [ $ this , 'on_term_meta_change ' ], 10 , 4 );
43+ add_action ( 'user_register ' , [ $ this , 'on_user_created ' ], 10 , 1 );
44+ add_action ( 'deleted_user ' , [ $ this , 'on_user_deleted ' ], 10 , 2 );
45+ add_action ( 'add_attachment ' , [ $ this , 'on_media_uploaded ' ], 10 , 1 );
46+ add_action ( 'edit_attachment ' , [ $ this , 'on_media_updated ' ], 10 , 1 );
47+ add_action ( 'delete_attachment ' , [ $ this , 'on_media_deleted ' ], 10 , 1 );
48+ add_action ( 'wp_insert_comment ' , [ $ this , 'on_comment_inserted ' ], 10 , 2 );
49+ add_action ( 'transition_comment_status ' , [ $ this , 'on_comment_status ' ], 10 , 3 );
50+ }
51+
52+ /**
53+ * Triggers webhooks for a given event if it is allowed.
54+ *
55+ * @param string $event
56+ * @param array $payload
57+ */
58+ private function trigger_webhooks ( string $ event , array $ payload ): void {
59+ $ allowed_events = $ this ->repository ->get_allowed_events ();
60+ if ( ! array_key_exists ( $ event , $ allowed_events ) ) {
61+ error_log ( 'Event ' . $ event . ' is not allowed. Allowed events: ' . implode ( ', ' , $ allowed_events ) );
62+ return ;
63+ }
64+
65+ do_action ( 'graphql_webhooks_before_trigger ' , $ event , $ payload );
66+ foreach ( $ this ->repository ->get_all () as $ webhook ) {
67+ if ( $ webhook ->event === $ event ) {
68+ $ this ->handler ->handle ( $ webhook , $ payload );
69+ }
70+ }
71+
72+ do_action ( 'graphql_webhooks_after_trigger ' , $ event , $ payload );
73+ }
74+
75+ /** Event Handlers **/
76+
77+ public function on_transition_post_status ( $ new_status , $ old_status , $ post ) {
78+ if ( $ old_status !== 'publish ' && $ new_status === 'publish ' ) {
79+ $ this ->trigger_webhooks ( 'post_published ' , [ 'post_id ' => $ post ->ID ] );
80+ }
81+ }
82+
83+ public function on_post_updated ( $ post_ID , $ post_after , $ post_before ) {
84+ $ this ->trigger_webhooks ( 'post_updated ' , [ 'post_id ' => $ post_ID ] );
85+
86+ if ( $ post_after ->post_author !== $ post_before ->post_author ) {
87+ $ this ->trigger_webhooks ( 'user_assigned ' , [
88+ 'post_id ' => $ post_ID ,
89+ 'author_id ' => $ post_after ->post_author ,
90+ ] );
91+
92+ $ this ->trigger_webhooks ( 'user_reassigned ' , [
93+ 'post_id ' => $ post_ID ,
94+ 'old_author_id ' => $ post_before ->post_author ,
95+ 'new_author_id ' => $ post_after ->post_author ,
96+ ] );
97+ }
98+ }
99+
100+ public function on_deleted_post ( $ post_ID , $ post ) {
101+ $ this ->trigger_webhooks ( 'post_deleted ' , [ 'post_id ' => $ post_ID ] );
102+ }
103+
104+ public function on_post_meta_change ( $ meta_id , $ post_id , $ meta_key , $ meta_value ) {
105+ $ this ->trigger_webhooks ( 'post_meta_change ' , [
106+ 'post_id ' => $ post_id ,
107+ 'meta_key ' => $ meta_key ,
108+ ] );
109+ }
110+
111+ public function on_term_created ( $ term_id , $ tt_id , $ taxonomy ) {
112+ $ this ->trigger_webhooks ( 'term_created ' , [
113+ 'term_id ' => $ term_id ,
114+ 'taxonomy ' => $ taxonomy ,
115+ ] );
116+ }
117+
118+ public function on_term_assigned ( $ object_id , $ terms , $ tt_ids , $ taxonomy , $ append , $ old_tt_ids ) {
119+ foreach ( (array ) $ terms as $ term_id ) {
120+ $ this ->trigger_webhooks ( 'term_assigned ' , [
121+ 'object_id ' => $ object_id ,
122+ 'term_id ' => $ term_id ,
123+ 'taxonomy ' => $ taxonomy ,
124+ ] );
125+ }
126+ }
127+
128+ public function on_term_unassigned ( $ object_id , $ taxonomy , $ term_ids ) {
129+ $ this ->trigger_webhooks ( 'term_unassigned ' , [
130+ 'object_id ' => $ object_id ,
131+ 'taxonomy ' => $ taxonomy ,
132+ 'term_ids ' => $ term_ids ,
133+ ] );
134+ }
135+
136+ public function on_term_deleted ( $ term , $ tt_id , $ taxonomy , $ deleted_term ) {
137+ $ this ->trigger_webhooks ( 'term_deleted ' , [
138+ 'term_id ' => $ term ,
139+ 'taxonomy ' => $ taxonomy ,
140+ ] );
141+ }
142+
143+ public function on_term_meta_change ( $ meta_id , $ term_id , $ meta_key , $ meta_value ) {
144+ $ this ->trigger_webhooks ( 'term_meta_change ' , [
145+ 'term_id ' => $ term_id ,
146+ 'meta_key ' => $ meta_key ,
147+ ] );
148+ }
149+
150+ public function on_user_created ( $ user_id ) {
151+ $ this ->trigger_webhooks ( 'user_created ' , [ 'user_id ' => $ user_id ] );
152+ }
153+
154+ public function on_user_deleted ( $ user_id , $ reassign ) {
155+ $ this ->trigger_webhooks ( 'user_deleted ' , [ 'user_id ' => $ user_id ] );
156+ }
157+
158+ public function on_media_uploaded ( $ post_id ) {
159+ $ this ->trigger_webhooks ( 'media_uploaded ' , [ 'post_id ' => $ post_id ] );
160+ }
161+
162+ public function on_media_updated ( $ post_id ) {
163+ $ this ->trigger_webhooks ( 'media_updated ' , [ 'post_id ' => $ post_id ] );
164+ }
165+
166+ public function on_media_deleted ( $ post_id ) {
167+ $ this ->trigger_webhooks ( 'media_deleted ' , [ 'post_id ' => $ post_id ] );
168+ }
169+
170+ public function on_comment_inserted ( $ comment_id , $ comment_object ) {
171+ $ this ->trigger_webhooks ( 'comment_inserted ' , [ 'comment_id ' => $ comment_id ] );
172+ }
173+
174+ public function on_comment_status ( $ new_status , $ old_status , $ comment ) {
175+ $ this ->trigger_webhooks ( 'comment_status ' , [
176+ 'comment_id ' => $ comment ->comment_ID ,
177+ 'new_status ' => $ new_status ,
178+ ] );
179+ }
180+ }
0 commit comments