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-core / api / webhooks.php

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

309 lines 6.6 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\AdminCore\Api;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 use CartflowsAdmin\AdminCore\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 \Cartflows_Helper::set_analytics_flag( 'first_webhook_configured' );
184
185 return new \WP_REST_Response(
186 array(
187 'success' => true,
188 'webhook' => $webhook,
189 ),
190 201
191 );
192 }
193
194 /**
195 * Update a webhook.
196 *
197 * @param \WP_REST_Request $request Full details about the request.
198 * @return \WP_REST_Response
199 */
200 public function update_webhook( $request ) {
201 $manager = \Cartflows_Webhook_Manager::get_instance();
202 $id = $request->get_param( 'id' );
203 $data = $request->get_json_params();
204
205 if ( empty( $data ) ) {
206 $data = $request->get_params();
207 }
208
209 if ( isset( $data['url'] ) && ! wp_http_validate_url( esc_url_raw( $data['url'] ) ) ) {
210 return new \WP_REST_Response(
211 array(
212 'success' => false,
213 'message' => __( 'Invalid webhook URL.', 'cartflows' ),
214 ),
215 400
216 );
217 }
218
219 $webhook = $manager->update_webhook( $id, $data );
220
221 if ( ! $webhook ) {
222 return new \WP_REST_Response(
223 array(
224 'success' => false,
225 'message' => __( 'Webhook not found.', 'cartflows' ),
226 ),
227 404
228 );
229 }
230
231 return new \WP_REST_Response(
232 array(
233 'success' => true,
234 'webhook' => $webhook,
235 ),
236 200
237 );
238 }
239
240 /**
241 * Delete a webhook.
242 *
243 * @param \WP_REST_Request $request Full details about the request.
244 * @return \WP_REST_Response
245 */
246 public function delete_webhook( $request ) {
247 $manager = \Cartflows_Webhook_Manager::get_instance();
248 $id = $request->get_param( 'id' );
249 $deleted = $manager->delete_webhook( $id );
250
251 if ( ! $deleted ) {
252 return new \WP_REST_Response(
253 array(
254 'success' => false,
255 'message' => __( 'Webhook not found.', 'cartflows' ),
256 ),
257 404
258 );
259 }
260
261 return new \WP_REST_Response(
262 array(
263 'success' => true,
264 'message' => __( 'Webhook deleted successfully.', 'cartflows' ),
265 ),
266 200
267 );
268 }
269
270 /**
271 * Send a test webhook.
272 *
273 * @param \WP_REST_Request $request Full details about the request.
274 * @return \WP_REST_Response
275 */
276 public function test_webhook( $request ) {
277 $id = $request->get_param( 'id' );
278 $dispatcher = \Cartflows_Webhook_Dispatcher::get_instance();
279 $result = $dispatcher->send_test( $id );
280
281 return new \WP_REST_Response(
282 array(
283 'success' => $result['success'],
284 'response_code' => $result['response_code'],
285 'error' => $result['error'],
286 ),
287 200
288 );
289 }
290
291 /**
292 * Get available webhook events.
293 *
294 * @param \WP_REST_Request $request Full details about the request.
295 * @return \WP_REST_Response
296 */
297 public function get_events( $request ) {
298 $events = \Cartflows_Webhook_Events::get_available_events();
299
300 return new \WP_REST_Response(
301 array(
302 'success' => true,
303 'events' => $events,
304 ),
305 200
306 );
307 }
308 }
309