PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.4
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.4
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / admin-legacy-core / api / webhooks.php

webhooks.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.4, at admin-legacy-core/api/webhooks.php

307 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Webhooks REST API Controller.
4 *
5 * @package CartFlows
6 */
7
8 namespace CartflowsAdmin\AdminLegacyCore\Api;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 use CartflowsAdmin\AdminLegacyCore\Api\ApiBase;
16
17 /**
18 * Class Webhooks.
19 */
20 class Webhooks extends ApiBase {
21
22 /**
23 * Route base.
24 *
25 * @var string
26 */
27 protected $rest_base = '/admin/webhooks/';
28
29 /**
30 * Instance
31 *
32 * @access private
33 * @var object Class object.
34 * @since 1.0.0
35 */
36 private static $instance;
37
38 /**
39 * Initiator
40 *
41 * @since 1.0.0
42 * @return object initialized object of class.
43 */
44 public static function get_instance() {
45 if ( ! isset( self::$instance ) ) {
46 self::$instance = new self();
47 }
48 return self::$instance;
49 }
50
51 /**
52 * Register API routes.
53 */
54 public function register_routes() {
55
56 $namespace = $this->get_api_namespace();
57
58 // List + Create webhooks.
59 register_rest_route(
60 $namespace,
61 $this->rest_base,
62 array(
63 array(
64 'methods' => \WP_REST_Server::READABLE,
65 'callback' => array( $this, 'get_webhooks' ),
66 'permission_callback' => array( $this, 'permissions_check' ),
67 ),
68 array(
69 'methods' => \WP_REST_Server::CREATABLE,
70 'callback' => array( $this, 'create_webhook' ),
71 'permission_callback' => array( $this, 'permissions_check' ),
72 ),
73 )
74 );
75
76 // Update + Delete a webhook.
77 register_rest_route(
78 $namespace,
79 $this->rest_base . '(?P<id>[a-zA-Z0-9-]+)',
80 array(
81 array(
82 'methods' => 'PUT',
83 'callback' => array( $this, 'update_webhook' ),
84 'permission_callback' => array( $this, 'permissions_check' ),
85 ),
86 array(
87 'methods' => \WP_REST_Server::DELETABLE,
88 'callback' => array( $this, 'delete_webhook' ),
89 'permission_callback' => array( $this, 'permissions_check' ),
90 ),
91 )
92 );
93
94 // Send test webhook.
95 register_rest_route(
96 $namespace,
97 $this->rest_base . '(?P<id>[a-zA-Z0-9-]+)/test',
98 array(
99 array(
100 'methods' => \WP_REST_Server::CREATABLE,
101 'callback' => array( $this, 'test_webhook' ),
102 'permission_callback' => array( $this, 'permissions_check' ),
103 ),
104 )
105 );
106
107 // Get available events.
108 register_rest_route(
109 $namespace,
110 $this->rest_base . 'events',
111 array(
112 array(
113 'methods' => \WP_REST_Server::READABLE,
114 'callback' => array( $this, 'get_events' ),
115 'permission_callback' => array( $this, 'permissions_check' ),
116 ),
117 )
118 );
119 }
120
121 /**
122 * Permission check.
123 *
124 * @param \WP_REST_Request $request Full details about the request.
125 * @return bool|\WP_Error
126 */
127 public function permissions_check( $request ) {
128 if ( ! current_user_can( 'cartflows_manage_settings' ) ) {
129 return new \WP_Error(
130 'cartflows_rest_cannot_manage',
131 __( 'Sorry, you are not allowed to manage webhooks.', 'cartflows' ),
132 array( 'status' => rest_authorization_required_code() )
133 );
134 }
135 return true;
136 }
137
138 /**
139 * Get all webhooks.
140 *
141 * @param \WP_REST_Request $request Full details about the request.
142 * @return \WP_REST_Response
143 */
144 public function get_webhooks( $request ) {
145 $manager = \Cartflows_Webhook_Manager::get_instance();
146 $webhooks = $manager->get_webhooks();
147
148 return new \WP_REST_Response(
149 array(
150 'success' => true,
151 'webhooks' => $webhooks,
152 ),
153 200
154 );
155 }
156
157 /**
158 * Create a webhook.
159 *
160 * @param \WP_REST_Request $request Full details about the request.
161 * @return \WP_REST_Response
162 */
163 public function create_webhook( $request ) {
164 $manager = \Cartflows_Webhook_Manager::get_instance();
165 $data = $request->get_json_params();
166
167 if ( empty( $data ) ) {
168 $data = $request->get_params();
169 }
170
171 $webhook = $manager->add_webhook( $data );
172
173 if ( ! $webhook ) {
174 return new \WP_REST_Response(
175 array(
176 'success' => false,
177 'message' => __( 'Invalid webhook URL.', 'cartflows' ),
178 ),
179 400
180 );
181 }
182
183 return new \WP_REST_Response(
184 array(
185 'success' => true,
186 'webhook' => $webhook,
187 ),
188 201
189 );
190 }
191
192 /**
193 * Update a webhook.
194 *
195 * @param \WP_REST_Request $request Full details about the request.
196 * @return \WP_REST_Response
197 */
198 public function update_webhook( $request ) {
199 $manager = \Cartflows_Webhook_Manager::get_instance();
200 $id = $request->get_param( 'id' );
201 $data = $request->get_json_params();
202
203 if ( empty( $data ) ) {
204 $data = $request->get_params();
205 }
206
207 if ( isset( $data['url'] ) && ! wp_http_validate_url( esc_url_raw( $data['url'] ) ) ) {
208 return new \WP_REST_Response(
209 array(
210 'success' => false,
211 'message' => __( 'Invalid webhook URL.', 'cartflows' ),
212 ),
213 400
214 );
215 }
216
217 $webhook = $manager->update_webhook( $id, $data );
218
219 if ( ! $webhook ) {
220 return new \WP_REST_Response(
221 array(
222 'success' => false,
223 'message' => __( 'Webhook not found.', 'cartflows' ),
224 ),
225 404
226 );
227 }
228
229 return new \WP_REST_Response(
230 array(
231 'success' => true,
232 'webhook' => $webhook,
233 ),
234 200
235 );
236 }
237
238 /**
239 * Delete a webhook.
240 *
241 * @param \WP_REST_Request $request Full details about the request.
242 * @return \WP_REST_Response
243 */
244 public function delete_webhook( $request ) {
245 $manager = \Cartflows_Webhook_Manager::get_instance();
246 $id = $request->get_param( 'id' );
247 $deleted = $manager->delete_webhook( $id );
248
249 if ( ! $deleted ) {
250 return new \WP_REST_Response(
251 array(
252 'success' => false,
253 'message' => __( 'Webhook not found.', 'cartflows' ),
254 ),
255 404
256 );
257 }
258
259 return new \WP_REST_Response(
260 array(
261 'success' => true,
262 'message' => __( 'Webhook deleted successfully.', 'cartflows' ),
263 ),
264 200
265 );
266 }
267
268 /**
269 * Send a test webhook.
270 *
271 * @param \WP_REST_Request $request Full details about the request.
272 * @return \WP_REST_Response
273 */
274 public function test_webhook( $request ) {
275 $id = $request->get_param( 'id' );
276 $dispatcher = \Cartflows_Webhook_Dispatcher::get_instance();
277 $result = $dispatcher->send_test( $id );
278
279 return new \WP_REST_Response(
280 array(
281 'success' => $result['success'],
282 'response_code' => $result['response_code'],
283 'error' => $result['error'],
284 ),
285 200
286 );
287 }
288
289 /**
290 * Get available webhook events.
291 *
292 * @param \WP_REST_Request $request Full details about the request.
293 * @return \WP_REST_Response
294 */
295 public function get_events( $request ) {
296 $events = \Cartflows_Webhook_Events::get_available_events();
297
298 return new \WP_REST_Response(
299 array(
300 'success' => true,
301 'events' => $events,
302 ),
303 200
304 );
305 }
306 }
307