PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.3
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / rest-api / Controllers / class-automation-controller.php

class-automation-controller.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at includes/core/rest-api/Controllers/class-automation-controller.php

442 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Automation REST API Controller
4 *
5 * @package WPFunnels
6 * @since 3.9.5
7 */
8
9 namespace WPFunnels\Rest\Controllers;
10
11 use WP_REST_Controller;
12 use WP_REST_Server;
13 use WP_Error;
14 use WP_REST_Request;
15 use WP_REST_Response;
16 use MintMail\App\Internal\Automation\AutomationModel;
17 use Mint\MRM\DataBase\Tables\AutomationMetaSchema;
18
19 /**
20 * Automation API Controller
21 */
22 class WpfnlAutomationController extends WP_REST_Controller
23 {
24 /**
25 * Namespace
26 *
27 * @var string
28 */
29 protected $namespace = 'wpfnl/v1';
30
31 /**
32 * Rest base
33 *
34 * @var string
35 */
36 protected $rest_base = 'automation';
37
38 /**
39 * Register routes
40 */
41 public function register_routes()
42 {
43 // Get all automations
44 register_rest_route(
45 $this->namespace,
46 '/' . $this->rest_base,
47 [
48 [
49 'methods' => WP_REST_Server::READABLE,
50 'callback' => [$this, 'get_items'],
51 'permission_callback' => [$this, 'get_items_permissions_check'],
52 ],
53 [
54 'methods' => WP_REST_Server::CREATABLE,
55 'callback' => [$this, 'create_item'],
56 'permission_callback' => [$this, 'create_item_permissions_check'],
57 ],
58 ]
59 );
60
61 // Listing for the automations screen: the funnel-sourced automations
62 // with their contact counts, paginated. Deliberately separate from
63 // get_items(), which hands back Mail Mint's own unfiltered list and is
64 // what the MCP automation tools already read.
65 register_rest_route(
66 $this->namespace,
67 '/' . $this->rest_base . '/list',
68 [
69 [
70 'methods' => WP_REST_Server::READABLE,
71 'callback' => [$this, 'get_list'],
72 'permission_callback' => [$this, 'get_items_permissions_check'],
73 'args' => [
74 'page' => [
75 'description' => __('Current page of the collection.', 'wpfnl'),
76 'type' => 'integer',
77 'default' => 1,
78 ],
79 'per_page' => [
80 'description' => __('Maximum number of items to be returned in the result set.', 'wpfnl'),
81 'type' => 'integer',
82 'default' => 10,
83 ],
84 ],
85 ],
86 ]
87 );
88
89 // Get, update, delete single automation
90 register_rest_route(
91 $this->namespace,
92 '/' . $this->rest_base . '/(?P<id>[\d]+)',
93 [
94 [
95 'methods' => WP_REST_Server::READABLE,
96 'callback' => [$this, 'get_item'],
97 'permission_callback' => [$this, 'get_item_permissions_check'],
98 'args' => [
99 'id' => [
100 'description' => __('Unique identifier for the automation.', 'wpfnl'),
101 'type' => 'integer',
102 ],
103 ],
104 ],
105 [
106 'methods' => WP_REST_Server::EDITABLE,
107 'callback' => [$this, 'update_item'],
108 'permission_callback' => [$this, 'update_item_permissions_check'],
109 'args' => [
110 'id' => [
111 'description' => __('Unique identifier for the automation.', 'wpfnl'),
112 'type' => 'integer',
113 ],
114 ],
115 ],
116 [
117 'methods' => WP_REST_Server::DELETABLE,
118 'callback' => [$this, 'delete_item'],
119 'permission_callback' => [$this, 'delete_item_permissions_check'],
120 'args' => [
121 'id' => [
122 'description' => __('Unique identifier for the automation.', 'wpfnl'),
123 'type' => 'integer',
124 ],
125 ],
126 ],
127 ]
128 );
129 }
130
131 /**
132 * Check permissions for getting items
133 *
134 * @param WP_REST_Request $request
135 * @return bool|WP_Error
136 */
137 public function get_items_permissions_check($request)
138 {
139 return current_user_can('manage_options');
140 }
141
142 /**
143 * Get all automations
144 *
145 * @param WP_REST_Request $request
146 * @return WP_REST_Response|WP_Error
147 */
148 public function get_items($request)
149 {
150 $page = $request->get_param('page') ?: 1;
151 $per_page = $request->get_param('per_page') ?: 10;
152
153 // Get automations from Mail Mint if available
154 if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) {
155 $offset = ($page - 1) * $per_page;
156 $automations = AutomationModel::get_all('created_at', 'DESC', $offset, $per_page, '', 'all');
157
158 return new WP_REST_Response($automations, 200);
159 }
160
161 // Fallback if Mail Mint is not available
162 return new WP_REST_Response([
163 'data' => [],
164 'total' => 0,
165 'total_pages' => 0,
166 'per_page' => $per_page
167 ], 200);
168 }
169
170 /**
171 * List the automations belonging to WPFunnels.
172 *
173 * Same data the legacy automations screen rendered server-side: the
174 * funnel-sourced automations only, each with the contact counts the list
175 * shows, wrapped in pagination. The query itself lives behind
176 * `wpfunnels/retrieve_all_automations`, which WPFunnels Pro implements, so
177 * without Pro this answers with an empty page rather than an error.
178 *
179 * @param WP_REST_Request $request Request object.
180 * @return WP_REST_Response
181 * @since 3.13.3
182 */
183 public function get_list($request)
184 {
185 $page = max(1, (int) $request->get_param('page'));
186 $per_page = max(1, (int) $request->get_param('per_page'));
187
188 $result = \WPFunnels\Wpfnl_functions::retrieve_all_automations($page, $per_page);
189
190 return new WP_REST_Response(
191 [
192 'success' => true,
193 'data' => isset($result['data']) ? array_values($result['data']) : [],
194 'total' => isset($result['total']) ? (int) $result['total'] : 0,
195 'total_pages' => isset($result['total_pages']) ? (int) $result['total_pages'] : 0,
196 'page' => $page,
197 'per_page' => $per_page,
198 ],
199 200
200 );
201 }
202
203
204 /**
205 * Check permissions for creating item
206 *
207 * @param WP_REST_Request $request
208 * @return bool|WP_Error
209 */
210 public function create_item_permissions_check($request)
211 {
212 return current_user_can('manage_options');
213 }
214
215 /**
216 * Create automation
217 *
218 * @param WP_REST_Request $request
219 * @return WP_REST_Response|WP_Error
220 */
221 public function create_item($request)
222 {
223 $data = $request->get_json_params();
224
225 // Use Mail Mint automation creation if available
226 if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) {
227 if (empty($data['author'])) {
228 $data['author'] = get_current_user_id();
229 }
230 $automation_id = AutomationModel::get_instance()->create_or_update($data);
231
232 if (!$automation_id) {
233 return new WP_Error(
234 'wpfnl_automation_create_failed',
235 __('Failed to create automation.', 'wpfnl'),
236 ['status' => 500]
237 );
238 }
239
240 $this->update_meta($automation_id, 'source', 'wpf');
241
242 return new WP_REST_Response(['id' => $automation_id], 201);
243 }
244
245 // Fallback
246 return new WP_Error(
247 'wpfnl_automation_not_available',
248 __('Mail Mint is required for automation functionality.', 'wpfnl'),
249 ['status' => 400]
250 );
251 }
252
253 /**
254 * Check permissions for getting single item
255 *
256 * @param WP_REST_Request $request
257 * @return bool|WP_Error
258 */
259 public function get_item_permissions_check($request)
260 {
261 return current_user_can('manage_options');
262 }
263
264 /**
265 * Get single automation
266 *
267 * @param WP_REST_Request $request
268 * @return WP_REST_Response|WP_Error
269 */
270 public function get_item($request)
271 {
272 $id = $request->get_param('id');
273
274 // Get automation from Mail Mint if available
275 if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) {
276 $automation = AutomationModel::get_single($id);
277
278 if (empty($automation)) {
279 return new WP_Error(
280 'wpfnl_automation_not_found',
281 __('Automation not found.', 'wpfnl'),
282 ['status' => 404]
283 );
284 }
285
286 return new WP_REST_Response($automation, 200);
287 }
288
289 return new WP_Error(
290 'wpfnl_automation_not_found',
291 __('Automation not found.', 'wpfnl'),
292 ['status' => 404]
293 );
294 }
295
296 /**
297 * Check permissions for updating item
298 *
299 * @param WP_REST_Request $request
300 * @return bool|WP_Error
301 */
302 public function update_item_permissions_check($request)
303 {
304 return current_user_can('manage_options');
305 }
306
307 /**
308 * Update automation
309 *
310 * @param WP_REST_Request $request
311 * @return WP_REST_Response|WP_Error
312 */
313 public function update_item($request)
314 {
315 $id = $request->get_param('id');
316 $data = $request->get_json_params();
317
318 // Update automation via Mail Mint if available
319 if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) {
320 $data['id'] = $id;
321 $automation_id = AutomationModel::get_instance()->create_or_update($data);
322
323 if (!$automation_id) {
324 return new WP_Error(
325 'wpfnl_automation_update_failed',
326 __('Failed to update automation.', 'wpfnl'),
327 ['status' => 500]
328 );
329 }
330
331 return new WP_REST_Response(['id' => $automation_id], 200);
332 }
333
334 return new WP_Error(
335 'wpfnl_automation_not_available',
336 __('Mail Mint is required for automation functionality.', 'wpfnl'),
337 ['status' => 400]
338 );
339 }
340
341 /**
342 * Check permissions for deleting item
343 *
344 * @param WP_REST_Request $request
345 * @return bool|WP_Error
346 */
347 public function delete_item_permissions_check($request)
348 {
349 return current_user_can('manage_options');
350 }
351
352 /**
353 * Update/insert automation meta
354 *
355 * @param int $automation_id
356 * @param string $meta_key
357 * @param string $meta_value
358 * @return bool|int
359 */
360 public function update_meta($automation_id, $meta_key, $meta_value)
361 {
362 global $wpdb;
363 $automation_meta_table = $wpdb->prefix . AutomationMetaSchema::$table_name;
364
365 $select_query = $wpdb->prepare(
366 "SELECT * FROM $automation_meta_table WHERE automation_id = %d AND meta_key = %s",
367 array($automation_id, $meta_key)
368 );
369 $results = $wpdb->get_results($select_query);
370
371 if ($results) {
372 try {
373 $payload = [
374 'id' => isset($results[0]->id) ? $results[0]->id : '',
375 'meta_key' => $meta_key,
376 'meta_value' => $meta_value,
377 'updated_at' => current_time('mysql'),
378 ];
379 $updated = $wpdb->update(
380 $automation_meta_table,
381 $payload,
382 array('id' => $payload['id'])
383 );
384 return $updated ? true : false;
385 } catch (\Exception $e) {
386 return false;
387 }
388 } else {
389 try {
390 $wpdb->insert(
391 $automation_meta_table,
392 array(
393 'automation_id' => $automation_id,
394 'meta_key' => $meta_key,
395 'meta_value' => $meta_value,
396 'created_at' => current_time('mysql'),
397 'updated_at' => current_time('mysql'),
398 )
399 );
400 return $wpdb->insert_id;
401 } catch (\Exception $e) {
402 return false;
403 }
404 }
405 }
406
407 /**
408 * Delete automation
409 *
410 * @param WP_REST_Request $request
411 * @return WP_REST_Response|WP_Error
412 */
413 public function delete_item($request)
414 {
415 $id = $request->get_param('id');
416
417 // Delete automation via Mail Mint if available
418 if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) {
419 $result = AutomationModel::destroy($id);
420
421 if (!$result) {
422 return new WP_Error(
423 'wpfnl_automation_not_found',
424 __('Automation not found or could not be deleted.', 'wpfnl'),
425 ['status' => 404]
426 );
427 }
428
429 return new WP_REST_Response([
430 'message' => __('Automation deleted successfully.', 'wpfnl'),
431 'id' => $id
432 ], 200);
433 }
434
435 return new WP_Error(
436 'wpfnl_automation_not_available',
437 __('Mail Mint is required for automation functionality.', 'wpfnl'),
438 ['status' => 400]
439 );
440 }
441 }
442