| 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 |
// Get, update, delete single automation |
| 62 |
register_rest_route( |
| 63 |
$this->namespace, |
| 64 |
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
| 65 |
[ |
| 66 |
[ |
| 67 |
'methods' => WP_REST_Server::READABLE, |
| 68 |
'callback' => [$this, 'get_item'], |
| 69 |
'permission_callback' => [$this, 'get_item_permissions_check'], |
| 70 |
'args' => [ |
| 71 |
'id' => [ |
| 72 |
'description' => __('Unique identifier for the automation.', 'wpfnl'), |
| 73 |
'type' => 'integer', |
| 74 |
], |
| 75 |
], |
| 76 |
], |
| 77 |
[ |
| 78 |
'methods' => WP_REST_Server::EDITABLE, |
| 79 |
'callback' => [$this, 'update_item'], |
| 80 |
'permission_callback' => [$this, 'update_item_permissions_check'], |
| 81 |
'args' => [ |
| 82 |
'id' => [ |
| 83 |
'description' => __('Unique identifier for the automation.', 'wpfnl'), |
| 84 |
'type' => 'integer', |
| 85 |
], |
| 86 |
], |
| 87 |
], |
| 88 |
[ |
| 89 |
'methods' => WP_REST_Server::DELETABLE, |
| 90 |
'callback' => [$this, 'delete_item'], |
| 91 |
'permission_callback' => [$this, 'delete_item_permissions_check'], |
| 92 |
'args' => [ |
| 93 |
'id' => [ |
| 94 |
'description' => __('Unique identifier for the automation.', 'wpfnl'), |
| 95 |
'type' => 'integer', |
| 96 |
], |
| 97 |
], |
| 98 |
], |
| 99 |
] |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Check permissions for getting items |
| 105 |
* |
| 106 |
* @param WP_REST_Request $request |
| 107 |
* @return bool|WP_Error |
| 108 |
*/ |
| 109 |
public function get_items_permissions_check($request) |
| 110 |
{ |
| 111 |
return current_user_can('manage_options'); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get all automations |
| 116 |
* |
| 117 |
* @param WP_REST_Request $request |
| 118 |
* @return WP_REST_Response|WP_Error |
| 119 |
*/ |
| 120 |
public function get_items($request) |
| 121 |
{ |
| 122 |
$page = $request->get_param('page') ?: 1; |
| 123 |
$per_page = $request->get_param('per_page') ?: 10; |
| 124 |
|
| 125 |
// Get automations from Mail Mint if available |
| 126 |
if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) { |
| 127 |
$offset = ($page - 1) * $per_page; |
| 128 |
$automations = AutomationModel::get_all('created_at', 'DESC', $offset, $per_page, '', 'all'); |
| 129 |
|
| 130 |
return new WP_REST_Response($automations, 200); |
| 131 |
} |
| 132 |
|
| 133 |
// Fallback if Mail Mint is not available |
| 134 |
return new WP_REST_Response([ |
| 135 |
'data' => [], |
| 136 |
'total' => 0, |
| 137 |
'total_pages' => 0, |
| 138 |
'per_page' => $per_page |
| 139 |
], 200); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Check permissions for creating item |
| 144 |
* |
| 145 |
* @param WP_REST_Request $request |
| 146 |
* @return bool|WP_Error |
| 147 |
*/ |
| 148 |
public function create_item_permissions_check($request) |
| 149 |
{ |
| 150 |
return current_user_can('manage_options'); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Create automation |
| 155 |
* |
| 156 |
* @param WP_REST_Request $request |
| 157 |
* @return WP_REST_Response|WP_Error |
| 158 |
*/ |
| 159 |
public function create_item($request) |
| 160 |
{ |
| 161 |
$data = $request->get_json_params(); |
| 162 |
|
| 163 |
// Use Mail Mint automation creation if available |
| 164 |
if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) { |
| 165 |
if (empty($data['author'])) { |
| 166 |
$data['author'] = get_current_user_id(); |
| 167 |
} |
| 168 |
$automation_id = AutomationModel::get_instance()->create_or_update($data); |
| 169 |
|
| 170 |
if (!$automation_id) { |
| 171 |
return new WP_Error( |
| 172 |
'wpfnl_automation_create_failed', |
| 173 |
__('Failed to create automation.', 'wpfnl'), |
| 174 |
['status' => 500] |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
$this->update_meta($automation_id, 'source', 'wpf'); |
| 179 |
|
| 180 |
return new WP_REST_Response(['id' => $automation_id], 201); |
| 181 |
} |
| 182 |
|
| 183 |
// Fallback |
| 184 |
return new WP_Error( |
| 185 |
'wpfnl_automation_not_available', |
| 186 |
__('Mail Mint is required for automation functionality.', 'wpfnl'), |
| 187 |
['status' => 400] |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Check permissions for getting single item |
| 193 |
* |
| 194 |
* @param WP_REST_Request $request |
| 195 |
* @return bool|WP_Error |
| 196 |
*/ |
| 197 |
public function get_item_permissions_check($request) |
| 198 |
{ |
| 199 |
return current_user_can('manage_options'); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get single automation |
| 204 |
* |
| 205 |
* @param WP_REST_Request $request |
| 206 |
* @return WP_REST_Response|WP_Error |
| 207 |
*/ |
| 208 |
public function get_item($request) |
| 209 |
{ |
| 210 |
$id = $request->get_param('id'); |
| 211 |
|
| 212 |
// Get automation from Mail Mint if available |
| 213 |
if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) { |
| 214 |
$automation = AutomationModel::get_single($id); |
| 215 |
|
| 216 |
if (empty($automation)) { |
| 217 |
return new WP_Error( |
| 218 |
'wpfnl_automation_not_found', |
| 219 |
__('Automation not found.', 'wpfnl'), |
| 220 |
['status' => 404] |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
return new WP_REST_Response($automation, 200); |
| 225 |
} |
| 226 |
|
| 227 |
return new WP_Error( |
| 228 |
'wpfnl_automation_not_found', |
| 229 |
__('Automation not found.', 'wpfnl'), |
| 230 |
['status' => 404] |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Check permissions for updating item |
| 236 |
* |
| 237 |
* @param WP_REST_Request $request |
| 238 |
* @return bool|WP_Error |
| 239 |
*/ |
| 240 |
public function update_item_permissions_check($request) |
| 241 |
{ |
| 242 |
return current_user_can('manage_options'); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Update automation |
| 247 |
* |
| 248 |
* @param WP_REST_Request $request |
| 249 |
* @return WP_REST_Response|WP_Error |
| 250 |
*/ |
| 251 |
public function update_item($request) |
| 252 |
{ |
| 253 |
$id = $request->get_param('id'); |
| 254 |
$data = $request->get_json_params(); |
| 255 |
|
| 256 |
// Update automation via Mail Mint if available |
| 257 |
if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) { |
| 258 |
$data['id'] = $id; |
| 259 |
$automation_id = AutomationModel::get_instance()->create_or_update($data); |
| 260 |
|
| 261 |
if (!$automation_id) { |
| 262 |
return new WP_Error( |
| 263 |
'wpfnl_automation_update_failed', |
| 264 |
__('Failed to update automation.', 'wpfnl'), |
| 265 |
['status' => 500] |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
return new WP_REST_Response(['id' => $automation_id], 200); |
| 270 |
} |
| 271 |
|
| 272 |
return new WP_Error( |
| 273 |
'wpfnl_automation_not_available', |
| 274 |
__('Mail Mint is required for automation functionality.', 'wpfnl'), |
| 275 |
['status' => 400] |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Check permissions for deleting item |
| 281 |
* |
| 282 |
* @param WP_REST_Request $request |
| 283 |
* @return bool|WP_Error |
| 284 |
*/ |
| 285 |
public function delete_item_permissions_check($request) |
| 286 |
{ |
| 287 |
return current_user_can('manage_options'); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Update/insert automation meta |
| 292 |
* |
| 293 |
* @param int $automation_id |
| 294 |
* @param string $meta_key |
| 295 |
* @param string $meta_value |
| 296 |
* @return bool|int |
| 297 |
*/ |
| 298 |
public function update_meta($automation_id, $meta_key, $meta_value) |
| 299 |
{ |
| 300 |
global $wpdb; |
| 301 |
$automation_meta_table = $wpdb->prefix . AutomationMetaSchema::$table_name; |
| 302 |
|
| 303 |
$select_query = $wpdb->prepare( |
| 304 |
"SELECT * FROM $automation_meta_table WHERE automation_id = %d AND meta_key = %s", |
| 305 |
array($automation_id, $meta_key) |
| 306 |
); |
| 307 |
$results = $wpdb->get_results($select_query); |
| 308 |
|
| 309 |
if ($results) { |
| 310 |
try { |
| 311 |
$payload = [ |
| 312 |
'id' => isset($results[0]->id) ? $results[0]->id : '', |
| 313 |
'meta_key' => $meta_key, |
| 314 |
'meta_value' => $meta_value, |
| 315 |
'updated_at' => current_time('mysql'), |
| 316 |
]; |
| 317 |
$updated = $wpdb->update( |
| 318 |
$automation_meta_table, |
| 319 |
$payload, |
| 320 |
array('id' => $payload['id']) |
| 321 |
); |
| 322 |
return $updated ? true : false; |
| 323 |
} catch (\Exception $e) { |
| 324 |
return false; |
| 325 |
} |
| 326 |
} else { |
| 327 |
try { |
| 328 |
$wpdb->insert( |
| 329 |
$automation_meta_table, |
| 330 |
array( |
| 331 |
'automation_id' => $automation_id, |
| 332 |
'meta_key' => $meta_key, |
| 333 |
'meta_value' => $meta_value, |
| 334 |
'created_at' => current_time('mysql'), |
| 335 |
'updated_at' => current_time('mysql'), |
| 336 |
) |
| 337 |
); |
| 338 |
return $wpdb->insert_id; |
| 339 |
} catch (\Exception $e) { |
| 340 |
return false; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Delete automation |
| 347 |
* |
| 348 |
* @param WP_REST_Request $request |
| 349 |
* @return WP_REST_Response|WP_Error |
| 350 |
*/ |
| 351 |
public function delete_item($request) |
| 352 |
{ |
| 353 |
$id = $request->get_param('id'); |
| 354 |
|
| 355 |
// Delete automation via Mail Mint if available |
| 356 |
if (class_exists('MintMail\\App\\Internal\\Automation\\AutomationModel')) { |
| 357 |
$result = AutomationModel::destroy($id); |
| 358 |
|
| 359 |
if (!$result) { |
| 360 |
return new WP_Error( |
| 361 |
'wpfnl_automation_not_found', |
| 362 |
__('Automation not found or could not be deleted.', 'wpfnl'), |
| 363 |
['status' => 404] |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
return new WP_REST_Response([ |
| 368 |
'message' => __('Automation deleted successfully.', 'wpfnl'), |
| 369 |
'id' => $id |
| 370 |
], 200); |
| 371 |
} |
| 372 |
|
| 373 |
return new WP_Error( |
| 374 |
'wpfnl_automation_not_available', |
| 375 |
__('Mail Mint is required for automation functionality.', 'wpfnl'), |
| 376 |
['status' => 400] |
| 377 |
); |
| 378 |
} |
| 379 |
} |
| 380 |
|