7
7
8
8
namespace BigCommerce \Container ;
9
9
10
+ use BigCommerce \Settings \Sections \Import as Import_Settings ;
10
11
use BigCommerce \Webhooks \Checkout_Complete_Webhook ;
11
12
use BigCommerce \Webhooks \Product_Inventory_Update_Webhook ;
12
13
use BigCommerce \Webhooks \Product_Update_Webhook ;
13
14
use BigCommerce \Webhooks \Product_Updater ;
15
+ use BigCommerce \Webhooks \Status ;
14
16
use BigCommerce \Webhooks \Webhook ;
15
17
use BigCommerce \Webhooks \Webhook_Cron_Tasks ;
16
18
use BigCommerce \Webhooks \Webhook_Listener ;
22
24
*/
23
25
class Webhooks extends Provider {
24
26
const WEBHOOKS = 'webhooks.webhooks ' ;
27
+ const WEBHOOKS_STATUS = 'webhooks.webhooks_status ' ;
25
28
const WEBHOOKS_LISTENER = 'webhooks.listener_webhook ' ;
26
29
const PRODUCT_UPDATE_WEBHOOK = 'webhooks.product_update_webhook ' ;
27
30
const PRODUCT_INVENTORY_UPDATE_WEBHOOK = 'webhooks.inventory_update_webhook ' ;
@@ -32,12 +35,19 @@ class Webhooks extends Provider {
32
35
33
36
public function register ( Container $ container ) {
34
37
$ this ->declare_webhooks ( $ container );
38
+ $ this ->status ( $ container );
35
39
$ this ->register_webhooks ( $ container );
40
+
41
+
36
42
$ this ->handle_requests ( $ container );
37
43
$ this ->webhook_actions ( $ container );
38
44
$ this ->cron_actions ( $ container );
39
45
}
40
46
47
+ private function webhooks_enabled () {
48
+ return get_option ( Import_Settings::ENABLE_WEBHOOKS , 1 );
49
+ }
50
+
41
51
/**
42
52
* Declare all of the webhooks that will be registered by the plugin.
43
53
* The list of Webhook instances should be returned in $container[ self::WEBHOOKS ]
@@ -75,6 +85,24 @@ private function declare_webhooks( Container $container ) {
75
85
};
76
86
}
77
87
88
+ private function status ( Container $ container ) {
89
+ $ container [ self ::WEBHOOKS_STATUS ] = function ( Container $ container ) {
90
+ return new Status ( $ container [ self ::WEBHOOKS ], $ container [ Api::FACTORY ]->webhooks () );
91
+ };
92
+
93
+ add_action ( 'update_option_ ' . Import_Settings::ENABLE_WEBHOOKS , function ( $ old_value , $ new_value , $ option_name ) use ($ container ) {
94
+ $ container [ self ::WEBHOOKS_STATUS ]->update_option ( $ old_value , $ new_value , $ option_name );
95
+ }, 10 , 3 );
96
+
97
+ add_action ( 'add_option_ ' . Import_Settings::ENABLE_WEBHOOKS , function ( $ option_name , $ value ) use ($ container ) {
98
+ $ container [ self ::WEBHOOKS_STATUS ]->update_option ( null , $ value , $ option_name );
99
+ }, 10 , 2 );
100
+
101
+ add_filter ( 'bigcommerce/diagnostics ' , $ this ->create_callback ( 'webhook_diagnostics ' , function ( $ data ) use ( $ container ) {
102
+ return $ container [ self ::WEBHOOKS_STATUS ]->diagnostic_data ( $ data );
103
+ } ), 10 , 1 );
104
+ }
105
+
78
106
/**
79
107
* Register the webhooks with the BigCommerce API
80
108
*
@@ -87,7 +115,7 @@ private function register_webhooks( Container $container ) {
87
115
return new Webhook_Versioning ( $ container [ self ::WEBHOOKS ] );
88
116
};
89
117
90
- add_action ( 'bigcommerce/import/fetched_store_settings ' , $ this ->create_callback ( 'check_and_update_webhooks_version ' , function () use ( $ container ) {
118
+ add_action ( 'bigcommerce/settings/webhoooks_updated ' , $ this ->create_callback ( 'check_and_update_webhooks_version ' , function () use ( $ container ) {
91
119
$ container [ self ::WEBHOOKS_VERSIONING ]->maybe_update_webhooks ();
92
120
} ), 10 , 0 );
93
121
}
@@ -104,6 +132,10 @@ private function handle_requests( Container $container ) {
104
132
return new Webhook_Listener ( $ container [ self ::WEBHOOKS ] );
105
133
};
106
134
135
+ if ( ! $ this ->webhooks_enabled () ) {
136
+ return ;
137
+ }
138
+
107
139
// Listener for all webhook actions
108
140
add_action ( 'bigcommerce/action_endpoint/webhook ' , $ this ->create_callback ( 'webhook_listener ' , function ( $ args ) use ( $ container ) {
109
141
$ container [ self ::WEBHOOKS_LISTENER ]->handle_request ( $ args );
@@ -118,6 +150,10 @@ private function webhook_actions( Container $container ) {
118
150
return new Webhook_Cron_Tasks ();
119
151
};
120
152
153
+ if ( ! $ this ->webhooks_enabled () ) {
154
+ return ;
155
+ }
156
+
121
157
// Update product inventory webhook cron task
122
158
add_action ( 'bigcommerce/webhooks/product_inventory_updated ' , $ this ->create_callback ( 'check_and_update_product_inventory_task ' , function ( $ params ) use ( $ container ) {
123
159
$ container [ self ::WEBHOOKS_CRON_TASKS ]->set_product_update_cron_task ( $ params );
@@ -130,6 +166,10 @@ private function cron_actions( Container $container ) {
130
166
return new Product_Updater ( $ container [ Api::FACTORY ]->catalog (), $ container [ Api::FACTORY ]->channels () );
131
167
};
132
168
169
+ if ( ! $ this ->webhooks_enabled () ) {
170
+ return ;
171
+ }
172
+
133
173
add_action ( Webhook_Cron_Tasks::UPDATE_PRODUCT , $ this ->create_callback ( 'update_product_cron_handler ' , function ( $ product_id ) use ( $ container ) {
134
174
$ container [ self ::PRODUCT_UPDATER ]->update ( $ product_id );
135
175
} ), 10 , 1 );
0 commit comments