| 1 |
<?php |
| 2 |
namespace Ens\Flow; |
| 3 |
|
| 4 |
use Ens\Config; |
| 5 |
use Ens\Utils\Helpers; |
| 6 |
use WP_HTTP_Response; |
| 7 |
use WP_REST_Controller; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class FlowAPI |
| 11 |
* |
| 12 |
* @package Ens\Flow |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class FlowAPI extends WP_REST_Controller { |
| 17 |
|
| 18 |
/** |
| 19 |
* Namespace |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $namespace; |
| 26 |
|
| 27 |
/** |
| 28 |
* Rest base |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $rest_base = 'notification-flow'; |
| 35 |
|
| 36 |
protected $identifier; |
| 37 |
|
| 38 |
/** |
| 39 |
* FlowAPI constructor. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public function init($identifier) { |
| 44 |
$this->identifier = $identifier; |
| 45 |
$plugin_slug = Helpers::get_config_data( $this->identifier,'plugin_slug' ); |
| 46 |
$this->namespace = $plugin_slug . '/v1'; |
| 47 |
|
| 48 |
add_action( 'rest_api_init', [$this, 'register_routes'] ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register API endpoints routes. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function register_routes() { |
| 59 |
register_rest_route( |
| 60 |
$this->namespace, |
| 61 |
$this->rest_base, |
| 62 |
[ |
| 63 |
[ // Create |
| 64 |
'methods' => \WP_REST_Server::CREATABLE, |
| 65 |
'callback' => [$this, 'create_item'], |
| 66 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 67 |
], |
| 68 |
[ // Bulk delete |
| 69 |
'methods' => \WP_REST_Server::DELETABLE, |
| 70 |
'callback' => [$this, 'bulk_delete'], |
| 71 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 72 |
], |
| 73 |
[ // show list |
| 74 |
'methods' => \WP_REST_Server::READABLE, |
| 75 |
'callback' => [$this, 'get_items'], |
| 76 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 77 |
], |
| 78 |
] |
| 79 |
); |
| 80 |
|
| 81 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<flow_id>[\d]+)', [ |
| 82 |
[ // Get single flow |
| 83 |
'methods' => \WP_REST_Server::READABLE, |
| 84 |
'callback' => [$this, 'get_item'], |
| 85 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 86 |
], |
| 87 |
[ // Update single flow |
| 88 |
'methods' => \WP_REST_Server::EDITABLE, |
| 89 |
'callback' => [$this, 'update_item'], |
| 90 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 91 |
], |
| 92 |
[ // Delete single flow |
| 93 |
'methods' => \WP_REST_Server::DELETABLE, |
| 94 |
'callback' => [$this, 'delete_item'], |
| 95 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 96 |
], |
| 97 |
] ); |
| 98 |
|
| 99 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<flow_id>[\d]+)' . '/clone', [ |
| 100 |
[ // Clone flow |
| 101 |
'methods' => \WP_REST_Server::CREATABLE, |
| 102 |
'callback' => [$this, 'clone_item'], |
| 103 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 104 |
], |
| 105 |
] ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Check that the current user is allowed to manage notification flows. |
| 110 |
* |
| 111 |
* Every flow route is administrative: flows control the content and |
| 112 |
* recipients of the emails the site sends out. They must never be |
| 113 |
* reachable by unauthenticated visitors. |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
* |
| 117 |
* @param \WP_REST_Request $request |
| 118 |
* |
| 119 |
* @return bool |
| 120 |
*/ |
| 121 |
public function permissions_check( $request ) { |
| 122 |
$can = current_user_can( 'manage_options' ); |
| 123 |
|
| 124 |
return (bool) apply_filters( |
| 125 |
Helpers::get_hook_name( $this->identifier, 'ens_flow_permission' ), |
| 126 |
$can, |
| 127 |
$request |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Create flow |
| 133 |
* |
| 134 |
* @since 1.0.0 |
| 135 |
* |
| 136 |
* @param WP_Rest_Request $request |
| 137 |
* |
| 138 |
* @return JSON Created flow data |
| 139 |
*/ |
| 140 |
public function create_item( $request ) { |
| 141 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 142 |
if ( $response instanceof WP_HTTP_Response ) { |
| 143 |
return $response; |
| 144 |
} |
| 145 |
|
| 146 |
return $this->save_flow( $request ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Update flow |
| 151 |
* |
| 152 |
* @since 1.0.0 |
| 153 |
* |
| 154 |
* @param WP_Rest_Request $request |
| 155 |
* |
| 156 |
* @return JSON Updated flow data |
| 157 |
*/ |
| 158 |
public function update_item( $request ) { |
| 159 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 160 |
if ( $response instanceof WP_HTTP_Response ) { |
| 161 |
return $response; |
| 162 |
} |
| 163 |
|
| 164 |
$flow_id = (int) $request['flow_id']; |
| 165 |
$flow = new Flow( $this->identifier, $flow_id ); |
| 166 |
|
| 167 |
if ( !$flow->is_flow() ) { |
| 168 |
|
| 169 |
$response = [ |
| 170 |
'success' => 0, |
| 171 |
'status_code' => 422, |
| 172 |
'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 173 |
'data' => [], |
| 174 |
]; |
| 175 |
|
| 176 |
return new WP_HTTP_Response( $response, 422 ); |
| 177 |
} |
| 178 |
return $this->save_flow( $request, $flow_id ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get flow |
| 183 |
* |
| 184 |
* @since 1.0.0 |
| 185 |
* |
| 186 |
* @param WP_Rest_Request $request |
| 187 |
* |
| 188 |
* @return JSON Flow data |
| 189 |
*/ |
| 190 |
public function get_item( $request ) { |
| 191 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 192 |
if ( $response instanceof WP_HTTP_Response ) { |
| 193 |
return $response; |
| 194 |
} |
| 195 |
|
| 196 |
$flow_id = (int) $request['flow_id']; |
| 197 |
$flow = new Flow( $this->identifier, $flow_id ); |
| 198 |
|
| 199 |
if ( !$flow->is_flow() ) { |
| 200 |
|
| 201 |
$data = [ |
| 202 |
'success' => 0, |
| 203 |
'status_code' => 422, |
| 204 |
'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 205 |
'data' => [], |
| 206 |
]; |
| 207 |
|
| 208 |
return new WP_HTTP_Response( $data, 422 ); |
| 209 |
} |
| 210 |
|
| 211 |
$response = [ |
| 212 |
'success' => 1, |
| 213 |
'status_code' => 200, |
| 214 |
'message' => __( 'Successfully retrieved flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 215 |
'data' => $this->prepare_item( $flow_id ), |
| 216 |
]; |
| 217 |
|
| 218 |
return rest_ensure_response( $response, 200 ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get all flows |
| 223 |
* |
| 224 |
* @since 1.0.0 |
| 225 |
* |
| 226 |
* @param WP_Rest_Request $request |
| 227 |
* |
| 228 |
* @return JSON |
| 229 |
*/ |
| 230 |
public function get_items( $request ) { |
| 231 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 232 |
if ( $response instanceof WP_HTTP_Response ) { |
| 233 |
return $response; |
| 234 |
} |
| 235 |
|
| 236 |
$per_page = !empty( $request['per_page'] ) ? intval( $request['per_page'] ) : -1; |
| 237 |
$paged = !empty( $request['paged'] ) ? intval( $request['paged'] ) : -1; |
| 238 |
$trigger = !empty( $request['trigger'] ) ? sanitize_text_field( $request['trigger'] ) : ''; |
| 239 |
$status = !empty( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'any'; |
| 240 |
$search_key = !empty( $request['search_key'] ) ? sanitize_text_field( $request['search_key'] ) : ''; |
| 241 |
|
| 242 |
$flow = new Flow($this->identifier); |
| 243 |
$flows = $flow->all( [ |
| 244 |
'posts_per_page' => $per_page, |
| 245 |
'paged' => $paged, |
| 246 |
'trigger' => $trigger, |
| 247 |
'post_status' => $status, |
| 248 |
'search_key' => $search_key, |
| 249 |
] ); |
| 250 |
|
| 251 |
$items = []; |
| 252 |
|
| 253 |
foreach ( $flows['items'] as $item ) { |
| 254 |
$single_flow = $this->prepare_item( $item->ID ); |
| 255 |
$items[] = $single_flow; |
| 256 |
} |
| 257 |
|
| 258 |
$flow = new Flow( $this->identifier ); |
| 259 |
$status_count = $flow->total_flows_group_by_status(); |
| 260 |
|
| 261 |
$data = [ |
| 262 |
'success' => 1, |
| 263 |
'status_code' => 200, |
| 264 |
'message' => __( 'Showing filtered flow list.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 265 |
'data' => [ |
| 266 |
'total' => $flows['total'], |
| 267 |
'status_count' => $status_count, |
| 268 |
'items' => $items, |
| 269 |
], |
| 270 |
]; |
| 271 |
|
| 272 |
return rest_ensure_response( $data, 200 ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Delete flow |
| 277 |
* |
| 278 |
* @since 1.0.0 |
| 279 |
* |
| 280 |
* @param WP_Rest_Request $request |
| 281 |
* |
| 282 |
* @return JSON |
| 283 |
*/ |
| 284 |
public function delete_item( $request ) { |
| 285 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 286 |
if ( $response instanceof WP_HTTP_Response ) { |
| 287 |
return $response; |
| 288 |
} |
| 289 |
|
| 290 |
$flow_id = (int) $request['flow_id']; |
| 291 |
$flow = new Flow( $this->identifier, $flow_id ); |
| 292 |
|
| 293 |
if ( !$flow->is_flow() ) { |
| 294 |
|
| 295 |
$data = [ |
| 296 |
'success' => 0, |
| 297 |
'status_code' => 422, |
| 298 |
'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 299 |
'data' => [], |
| 300 |
]; |
| 301 |
|
| 302 |
return new WP_HTTP_Response( $data, 422 ); |
| 303 |
} |
| 304 |
|
| 305 |
$flow->delete(); |
| 306 |
|
| 307 |
$response = [ |
| 308 |
'success' => 1, |
| 309 |
'status_code' => 200, |
| 310 |
'message' => __( 'Successfully deleted flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 311 |
'data' => [], |
| 312 |
]; |
| 313 |
|
| 314 |
return rest_ensure_response( $response, 200 ); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Bulk delete flows |
| 319 |
* |
| 320 |
* @since 1.0.0 |
| 321 |
* |
| 322 |
* @param WP_Rest_Request $request |
| 323 |
* |
| 324 |
* @return JSON |
| 325 |
*/ |
| 326 |
public function bulk_delete( $request ) { |
| 327 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 328 |
if ( $response instanceof WP_HTTP_Response ) { |
| 329 |
return $response; |
| 330 |
} |
| 331 |
|
| 332 |
$flow_ids = json_decode( $request->get_body(), true ); |
| 333 |
|
| 334 |
foreach ( $flow_ids as $flow_id ) { |
| 335 |
$flow = new Flow( $this->identifier, $flow_id ); |
| 336 |
|
| 337 |
if ( !$flow->is_flow() ) { |
| 338 |
$data = [ |
| 339 |
'success' => 0, |
| 340 |
'status_code' => 422, |
| 341 |
'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 342 |
'data' => [], |
| 343 |
]; |
| 344 |
|
| 345 |
return new WP_HTTP_Response( $data, 422 ); |
| 346 |
} |
| 347 |
|
| 348 |
$flow->delete(); |
| 349 |
} |
| 350 |
|
| 351 |
return rest_ensure_response( [ |
| 352 |
'success' => 1, |
| 353 |
'status_code' => 200, |
| 354 |
'message' => __( 'Successfully deleted all flows', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 355 |
'data' => [ |
| 356 |
'items' => $flow_ids, |
| 357 |
], |
| 358 |
], 200 ); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Clone flow |
| 363 |
* |
| 364 |
* @since 1.0.0 |
| 365 |
* |
| 366 |
* @param Object $request |
| 367 |
* |
| 368 |
* @return JSON |
| 369 |
*/ |
| 370 |
public function clone_item( $request ) { |
| 371 |
$response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); |
| 372 |
if ( $response instanceof WP_HTTP_Response ) { |
| 373 |
return $response; |
| 374 |
} |
| 375 |
|
| 376 |
$flow_id = (int) $request['flow_id']; |
| 377 |
$flow = new Flow( $this->identifier, $flow_id ); |
| 378 |
|
| 379 |
if ( !$flow->is_flow() ) { |
| 380 |
return new WP_HTTP_Response( |
| 381 |
[ |
| 382 |
'success' => 0, |
| 383 |
'status_code' => 422, |
| 384 |
'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 385 |
'data' => [], |
| 386 |
], |
| 387 |
422 |
| 388 |
); |
| 389 |
} |
| 390 |
|
| 391 |
$flow->clone(); |
| 392 |
|
| 393 |
$item = $this->prepare_item( $flow ); |
| 394 |
|
| 395 |
$response = [ |
| 396 |
'success' => 1, |
| 397 |
'status_code' => 200, |
| 398 |
'message' => __( 'Successfully cloned requested flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 399 |
'data' => $item, |
| 400 |
]; |
| 401 |
|
| 402 |
return rest_ensure_response( $response, 200 ); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Save flow |
| 407 |
* |
| 408 |
* @since 1.0.0 |
| 409 |
* |
| 410 |
* @param WP_Rest_Request $request |
| 411 |
* @param int $id |
| 412 |
* |
| 413 |
* @return WP_REST_Response |
| 414 |
*/ |
| 415 |
public function save_flow( $request, $id = 0 ) { |
| 416 |
$flow = new Flow( $this->identifier, $id ); |
| 417 |
$request_body = $request->get_body(); |
| 418 |
$data = !is_null( $request_body ) ? json_decode( $request_body, true ) : []; |
| 419 |
|
| 420 |
// Check if the input data is empty |
| 421 |
if ( empty( $data ) ) { |
| 422 |
return rest_ensure_response( [ |
| 423 |
'status_code' => 400, |
| 424 |
'success' => 0, |
| 425 |
'message' => __( 'No data provided to save the flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 426 |
], 400 ); |
| 427 |
} |
| 428 |
|
| 429 |
$name = !empty( $data['name'] ) ? sanitize_text_field( $data['name'] ) : $flow->get_name(); |
| 430 |
$trigger = !empty( $data['trigger'] ) ? sanitize_text_field( $data['trigger'] ) : $flow->get_trigger(); |
| 431 |
|
| 432 |
$flow_config = !empty( $data['flow_config'] ) ? Helpers::ens_sanitize_recursive( $data['flow_config'] ) : $flow->get_flow_config(); |
| 433 |
$status = ( isset( $data['status'] ) && !empty( $data['status'] ) ) ? sanitize_text_field( strtolower( $data['status'] ) ) : ( $flow->get_status() ? $flow->get_status() : 'draft' ); |
| 434 |
|
| 435 |
$action = $id ? 'updated' : 'created'; |
| 436 |
|
| 437 |
// Check if the input data is empty |
| 438 |
if ( empty( $name ) || empty( $trigger ) ) { |
| 439 |
return rest_ensure_response( [ |
| 440 |
'status_code' => 400, |
| 441 |
'success' => 0, |
| 442 |
'message' => __( 'Flow name & trigger is required', Helpers::get_config_data( $this->identifier,'text_domain' ) ), |
| 443 |
], 400 ); |
| 444 |
} |
| 445 |
|
| 446 |
// preparing flow data. |
| 447 |
$flow_data = [ |
| 448 |
'name' => $name, |
| 449 |
'trigger' => $trigger, |
| 450 |
'flow_config' => $flow_config, |
| 451 |
'status' => $status, |
| 452 |
]; |
| 453 |
|
| 454 |
$flow->set_props( $flow_data ); |
| 455 |
$flow_id = $flow->save(); |
| 456 |
|
| 457 |
// Prepare response data. |
| 458 |
$item = $this->prepare_item( $flow_id ); |
| 459 |
|
| 460 |
$response = [ |
| 461 |
'status_code' => 200, |
| 462 |
'success' => 1, |
| 463 |
// translators: %s is the name of the action |
| 464 |
'message' => esc_html( sprintf( __( 'Successfully %s notification flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), $action ) ), |
| 465 |
'data' => $item, |
| 466 |
]; |
| 467 |
|
| 468 |
return rest_ensure_response( $response, 200 ); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Prepare item for response |
| 473 |
* |
| 474 |
* @since 1.0.0 |
| 475 |
* |
| 476 |
* @param integer $flow_id |
| 477 |
* |
| 478 |
* @return array |
| 479 |
*/ |
| 480 |
public function prepare_item( $flow_id ) { |
| 481 |
$flow = new Flow( $this->identifier, $flow_id ); |
| 482 |
$data = [ |
| 483 |
'id' => $flow->get_id(), |
| 484 |
'name' => $flow->get_name(), |
| 485 |
'trigger' => $flow->get_trigger(), |
| 486 |
'flow_config' => empty( $flow->get_flow_config() ) ? [] : $flow->get_flow_config(), |
| 487 |
'status' => $flow->get_status(), |
| 488 |
]; |
| 489 |
|
| 490 |
return apply_filters( Helpers::get_hook_name( $this->identifier, 'ens_flow_prepare_item' ), $data, $flow_id ); |
| 491 |
} |
| 492 |
} |