PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.1
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 / modules / webhook / classes / class-cartflows-webhook-manager.php

class-cartflows-webhook-manager.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.1, at modules/webhook/classes/class-cartflows-webhook-manager.php

241 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Webhook Manager.
4 *
5 * Handles CRUD operations for webhook configurations stored in wp_options.
6 *
7 * @package CartFlows
8 */
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 if ( ! class_exists( 'Cartflows_Webhook_Manager' ) ) {
16
17 /**
18 * Class Cartflows_Webhook_Manager.
19 */
20 class Cartflows_Webhook_Manager {
21
22 /**
23 * Option key for storing webhooks.
24 *
25 * @var string
26 */
27 const OPTION_KEY = '_cartflows_webhooks';
28
29 /**
30 * Member Variable
31 *
32 * @var instance
33 */
34 private static $instance = null;
35
36 /**
37 * Initiator
38 *
39 * @return object initialized object of class.
40 */
41 public static function get_instance() {
42 if ( is_null( self::$instance ) ) {
43 self::$instance = new self();
44 }
45 return self::$instance;
46 }
47
48 /**
49 * Get all webhooks.
50 *
51 * @return array
52 */
53 public function get_webhooks() {
54 $webhooks = get_option( self::OPTION_KEY, array() );
55 return is_array( $webhooks ) ? $webhooks : array();
56 }
57
58 /**
59 * Get a single webhook by ID.
60 *
61 * @param string $id Webhook ID.
62 * @return array|false
63 */
64 public function get_webhook( $id ) {
65 $webhooks = $this->get_webhooks();
66
67 foreach ( $webhooks as $webhook ) {
68 if ( isset( $webhook['id'] ) && $webhook['id'] === $id ) {
69 return $webhook;
70 }
71 }
72
73 return false;
74 }
75
76 /**
77 * Add a new webhook.
78 *
79 * @param array $data Webhook data.
80 * @return array|false The created webhook or false on failure.
81 */
82 public function add_webhook( $data ) {
83 $url = isset( $data['url'] ) ? esc_url_raw( $data['url'] ) : '';
84
85 if ( empty( $url ) || ! wp_http_validate_url( $url ) ) {
86 return false;
87 }
88
89 $webhook = array(
90 'id' => wp_generate_uuid4(),
91 'name' => isset( $data['name'] ) ? sanitize_text_field( $data['name'] ) : '',
92 'url' => $url,
93 'events' => isset( $data['events'] ) && is_array( $data['events'] ) ? array_map( 'sanitize_text_field', $data['events'] ) : array(),
94 'enabled' => isset( $data['enabled'] ) ? (bool) $data['enabled'] : true,
95 'secret' => wp_generate_password( 32, false ),
96 'max_retries' => isset( $data['max_retries'] ) ? absint( min( $data['max_retries'], 5 ) ) : 3,
97 'created_at' => current_time( 'mysql' ),
98 'updated_at' => current_time( 'mysql' ),
99 'last_delivery' => array(
100 'status' => '',
101 'triggered_at' => '',
102 'error' => '',
103 ),
104 );
105
106 $webhooks = $this->get_webhooks();
107 $webhooks[] = $webhook;
108
109 update_option( self::OPTION_KEY, $webhooks, false );
110
111 wcf()->logger->log( 'Webhook created: ' . $webhook['id'] . ' - ' . $webhook['name'] );
112
113 return $webhook;
114 }
115
116 /**
117 * Update an existing webhook.
118 *
119 * @param string $id Webhook ID.
120 * @param array $data Updated data.
121 * @return array|false The updated webhook or false on failure.
122 */
123 public function update_webhook( $id, $data ) {
124 $webhooks = $this->get_webhooks();
125 $updated = false;
126
127 foreach ( $webhooks as $index => $webhook ) {
128 if ( isset( $webhook['id'] ) && $webhook['id'] === $id ) {
129
130 if ( isset( $data['name'] ) ) {
131 $webhooks[ $index ]['name'] = sanitize_text_field( $data['name'] );
132 }
133
134 if ( isset( $data['url'] ) ) {
135 $url = esc_url_raw( $data['url'] );
136 if ( ! empty( $url ) && wp_http_validate_url( $url ) ) {
137 $webhooks[ $index ]['url'] = $url;
138 }
139 }
140
141 if ( isset( $data['events'] ) && is_array( $data['events'] ) ) {
142 $webhooks[ $index ]['events'] = array_map( 'sanitize_text_field', $data['events'] );
143 }
144
145 if ( isset( $data['enabled'] ) ) {
146 $webhooks[ $index ]['enabled'] = (bool) $data['enabled'];
147 }
148
149 if ( isset( $data['max_retries'] ) ) {
150 $webhooks[ $index ]['max_retries'] = absint( min( $data['max_retries'], 5 ) );
151 }
152
153 $webhooks[ $index ]['updated_at'] = current_time( 'mysql' );
154
155 $updated = $webhooks[ $index ];
156 break;
157 }
158 }
159
160 if ( $updated ) {
161 update_option( self::OPTION_KEY, $webhooks, false );
162 wcf()->logger->log( 'Webhook updated: ' . $id );
163 }
164
165 return $updated;
166 }
167
168 /**
169 * Delete a webhook.
170 *
171 * @param string $id Webhook ID.
172 * @return bool
173 */
174 public function delete_webhook( $id ) {
175 $webhooks = $this->get_webhooks();
176 $found = false;
177
178 foreach ( $webhooks as $index => $webhook ) {
179 if ( isset( $webhook['id'] ) && $webhook['id'] === $id ) {
180 unset( $webhooks[ $index ] );
181 $found = true;
182 break;
183 }
184 }
185
186 if ( $found ) {
187 update_option( self::OPTION_KEY, array_values( $webhooks ), false );
188 wcf()->logger->log( 'Webhook deleted: ' . $id );
189 return true;
190 }
191
192 return false;
193 }
194
195 /**
196 * Get all enabled webhooks for a specific event.
197 *
198 * @param string $event Event name.
199 * @return array
200 */
201 public function get_webhooks_for_event( $event ) {
202 $webhooks = $this->get_webhooks();
203 $matched = array();
204
205 foreach ( $webhooks as $webhook ) {
206 if ( ! empty( $webhook['enabled'] ) && isset( $webhook['events'] ) && in_array( $event, $webhook['events'], true ) ) {
207 $matched[] = $webhook;
208 }
209 }
210
211 return $matched;
212 }
213
214 /**
215 * Update last delivery status for a webhook.
216 *
217 * @param string $id Webhook ID.
218 * @param array $delivery_data Delivery status data.
219 * @return bool
220 */
221 public function update_last_delivery( $id, $delivery_data ) {
222 $webhooks = $this->get_webhooks();
223
224 foreach ( $webhooks as $index => $webhook ) {
225 if ( isset( $webhook['id'] ) && $webhook['id'] === $id ) {
226 $webhooks[ $index ]['last_delivery'] = array(
227 'status' => isset( $delivery_data['status'] ) ? sanitize_text_field( $delivery_data['status'] ) : '',
228 'triggered_at' => isset( $delivery_data['triggered_at'] ) ? sanitize_text_field( $delivery_data['triggered_at'] ) : current_time( 'mysql' ),
229 'error' => isset( $delivery_data['error'] ) ? sanitize_text_field( $delivery_data['error'] ) : '',
230 );
231
232 update_option( self::OPTION_KEY, $webhooks, false );
233 return true;
234 }
235 }
236
237 return false;
238 }
239 }
240 }
241