← All changes
|
vendor/themewinter/email-notification-sdk/src/Flow/FlowAPI.php
+42
-36
1.0.2
→
1.0.28
View file →
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | -namespace ENS\Flow; | |
| 2 | +namespace Ens\Flow; | |
| 3 | 3 | |
| 4 | -use ENS\Config; | |
| 4 | +use Ens\Config; | |
| 5 | +use Ens\Utils\Helpers; | |
| 5 | 6 | use WP_HTTP_Response; |
| 6 | 7 | use WP_REST_Controller; |
| 7 | 8 | |
| 8 | 9 | /** |
| @@ -7,9 +8,9 @@ | ||
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * Class FlowAPI |
| 10 | 11 | * |
| 11 | - * @package ENS\Flow | |
| 12 | + * @package Ens\Flow | |
| 12 | 13 | * |
| 13 | 14 | * @since 1.0.0 |
| 14 | 15 | */ |
| 15 | 16 | class FlowAPI extends WP_REST_Controller { |
| @@ -31,15 +32,18 @@ | ||
| 31 | 32 | * @var string |
| 32 | 33 | */ |
| 33 | 34 | protected $rest_base = 'notification-flow'; |
| 34 | 35 | |
| 36 | + protected $identifier; | |
| 37 | + | |
| 35 | 38 | /** |
| 36 | 39 | * FlowAPI constructor. |
| 37 | 40 | * |
| 38 | 41 | * @since 1.0.0 |
| 39 | 42 | */ |
| 40 | - public function init() { | |
| 41 | - $plugin_slug = Config::get( 'plugin_slug' ); | |
| 43 | + public function init($identifier) { | |
| 44 | + $this->identifier = $identifier; | |
| 45 | + $plugin_slug = Helpers::get_config_data( $this->identifier,'plugin_slug' ); | |
| 42 | 46 | $this->namespace = $plugin_slug . '/v1'; |
| 43 | 47 | |
| 44 | 48 | add_action( 'rest_api_init', [$this, 'register_routes'] ); |
| 45 | 49 | } |
| @@ -131,9 +135,9 @@ | ||
| 131 | 135 | * |
| 132 | 136 | * @return JSON Created flow data |
| 133 | 137 | */ |
| 134 | 138 | public function create_item( $request ) { |
| 135 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 139 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 136 | 140 | if ( $response instanceof WP_HTTP_Response ) { |
| 137 | 141 | return $response; |
| 138 | 142 | } |
| 139 | 143 | |
| @@ -149,15 +153,15 @@ | ||
| 149 | 153 | * |
| 150 | 154 | * @return JSON Updated flow data |
| 151 | 155 | */ |
| 152 | 156 | public function update_item( $request ) { |
| 153 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 157 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 154 | 158 | if ( $response instanceof WP_HTTP_Response ) { |
| 155 | 159 | return $response; |
| 156 | 160 | } |
| 157 | 161 | |
| 158 | 162 | $flow_id = (int) $request['flow_id']; |
| 159 | - $flow = new Flow( $flow_id ); | |
| 163 | + $flow = new Flow( $this->identifier, $flow_id ); | |
| 160 | 164 | |
| 161 | 165 | if ( !$flow->is_flow() ) { |
| 162 | 166 | |
| 163 | 167 | $response = [ |
| @@ -162,9 +166,9 @@ | ||
| 162 | 166 | |
| 163 | 167 | $response = [ |
| 164 | 168 | 'success' => 0, |
| 165 | 169 | 'status_code' => 422, |
| 166 | - 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ), | |
| 170 | + 'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 167 | 171 | 'data' => [], |
| 168 | 172 | ]; |
| 169 | 173 | |
| 170 | 174 | return new WP_HTTP_Response( $response, 422 ); |
| @@ -181,15 +185,15 @@ | ||
| 181 | 185 | * |
| 182 | 186 | * @return JSON Flow data |
| 183 | 187 | */ |
| 184 | 188 | public function get_item( $request ) { |
| 185 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 189 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 186 | 190 | if ( $response instanceof WP_HTTP_Response ) { |
| 187 | 191 | return $response; |
| 188 | 192 | } |
| 189 | 193 | |
| 190 | 194 | $flow_id = (int) $request['flow_id']; |
| 191 | - $flow = new Flow( $flow_id ); | |
| 195 | + $flow = new Flow( $this->identifier, $flow_id ); | |
| 192 | 196 | |
| 193 | 197 | if ( !$flow->is_flow() ) { |
| 194 | 198 | |
| 195 | 199 | $data = [ |
| @@ -194,9 +198,9 @@ | ||
| 194 | 198 | |
| 195 | 199 | $data = [ |
| 196 | 200 | 'success' => 0, |
| 197 | 201 | 'status_code' => 422, |
| 198 | - 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ), | |
| 202 | + 'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 199 | 203 | 'data' => [], |
| 200 | 204 | ]; |
| 201 | 205 | |
| 202 | 206 | return new WP_HTTP_Response( $data, 422 ); |
| @@ -204,9 +208,9 @@ | ||
| 204 | 208 | |
| 205 | 209 | $response = [ |
| 206 | 210 | 'success' => 1, |
| 207 | 211 | 'status_code' => 200, |
| 208 | - 'message' => __( 'Successfully retrieved flow', Config::get( 'text_domain' ) ), | |
| 212 | + 'message' => __( 'Successfully retrieved flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 209 | 213 | 'data' => $this->prepare_item( $flow_id ), |
| 210 | 214 | ]; |
| 211 | 215 | |
| 212 | 216 | return rest_ensure_response( $response, 200 ); |
| @@ -221,9 +225,9 @@ | ||
| 221 | 225 | * |
| 222 | 226 | * @return JSON |
| 223 | 227 | */ |
| 224 | 228 | public function get_items( $request ) { |
| 225 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 229 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 226 | 230 | if ( $response instanceof WP_HTTP_Response ) { |
| 227 | 231 | return $response; |
| 228 | 232 | } |
| 229 | 233 | |
| @@ -232,9 +236,10 @@ | ||
| 232 | 236 | $trigger = !empty( $request['trigger'] ) ? sanitize_text_field( $request['trigger'] ) : ''; |
| 233 | 237 | $status = !empty( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'any'; |
| 234 | 238 | $search_key = !empty( $request['search_key'] ) ? sanitize_text_field( $request['search_key'] ) : ''; |
| 235 | 239 | |
| 236 | - $flows = Flow::all( [ | |
| 240 | + $flow = new Flow($this->identifier); | |
| 241 | + $flows = $flow->all( [ | |
| 237 | 242 | 'posts_per_page' => $per_page, |
| 238 | 243 | 'paged' => $paged, |
| 239 | 244 | 'trigger' => $trigger, |
| 240 | 245 | 'post_status' => $status, |
| @@ -247,14 +252,15 @@ | ||
| 247 | 252 | $single_flow = $this->prepare_item( $item->ID ); |
| 248 | 253 | $items[] = $single_flow; |
| 249 | 254 | } |
| 250 | 255 | |
| 251 | - $status_count = Flow::total_flows_group_by_status(); | |
| 256 | + $flow = new Flow( $this->identifier ); | |
| 257 | + $status_count = $flow->total_flows_group_by_status(); | |
| 252 | 258 | |
| 253 | 259 | $data = [ |
| 254 | 260 | 'success' => 1, |
| 255 | 261 | 'status_code' => 200, |
| 256 | - 'message' => __( 'Showing filtered flow list.', Config::get( 'text_domain' ) ), | |
| 262 | + 'message' => __( 'Showing filtered flow list.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 257 | 263 | 'data' => [ |
| 258 | 264 | 'total' => $flows['total'], |
| 259 | 265 | 'status_count' => $status_count, |
| 260 | 266 | 'items' => $items, |
| @@ -273,15 +279,15 @@ | ||
| 273 | 279 | * |
| 274 | 280 | * @return JSON |
| 275 | 281 | */ |
| 276 | 282 | public function delete_item( $request ) { |
| 277 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 283 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 278 | 284 | if ( $response instanceof WP_HTTP_Response ) { |
| 279 | 285 | return $response; |
| 280 | 286 | } |
| 281 | 287 | |
| 282 | 288 | $flow_id = (int) $request['flow_id']; |
| 283 | - $flow = new Flow( $flow_id ); | |
| 289 | + $flow = new Flow( $this->identifier, $flow_id ); | |
| 284 | 290 | |
| 285 | 291 | if ( !$flow->is_flow() ) { |
| 286 | 292 | |
| 287 | 293 | $data = [ |
| @@ -286,9 +292,9 @@ | ||
| 286 | 292 | |
| 287 | 293 | $data = [ |
| 288 | 294 | 'success' => 0, |
| 289 | 295 | 'status_code' => 422, |
| 290 | - 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ), | |
| 296 | + 'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 291 | 297 | 'data' => [], |
| 292 | 298 | ]; |
| 293 | 299 | |
| 294 | 300 | return new WP_HTTP_Response( $data, 422 ); |
| @@ -298,9 +304,9 @@ | ||
| 298 | 304 | |
| 299 | 305 | $response = [ |
| 300 | 306 | 'success' => 1, |
| 301 | 307 | 'status_code' => 200, |
| 302 | - 'message' => __( 'Successfully deleted flow', Config::get( 'text_domain' ) ), | |
| 308 | + 'message' => __( 'Successfully deleted flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 303 | 309 | 'data' => [], |
| 304 | 310 | ]; |
| 305 | 311 | |
| 306 | 312 | return rest_ensure_response( $response, 200 ); |
| @@ -315,9 +321,9 @@ | ||
| 315 | 321 | * |
| 316 | 322 | * @return JSON |
| 317 | 323 | */ |
| 318 | 324 | public function bulk_delete( $request ) { |
| 319 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 325 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 320 | 326 | if ( $response instanceof WP_HTTP_Response ) { |
| 321 | 327 | return $response; |
| 322 | 328 | } |
| 323 | 329 | |
| @@ -323,15 +329,15 @@ | ||
| 323 | 329 | |
| 324 | 330 | $flow_ids = json_decode( $request->get_body(), true ); |
| 325 | 331 | |
| 326 | 332 | foreach ( $flow_ids as $flow_id ) { |
| 327 | - $flow = new Flow( $flow_id ); | |
| 333 | + $flow = new Flow( $this->identifier, $flow_id ); | |
| 328 | 334 | |
| 329 | 335 | if ( !$flow->is_flow() ) { |
| 330 | 336 | $data = [ |
| 331 | 337 | 'success' => 0, |
| 332 | 338 | 'status_code' => 422, |
| 333 | - 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ), | |
| 339 | + 'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 334 | 340 | 'data' => [], |
| 335 | 341 | ]; |
| 336 | 342 | |
| 337 | 343 | return new WP_HTTP_Response( $data, 422 ); |
| @@ -342,9 +348,9 @@ | ||
| 342 | 348 | |
| 343 | 349 | return rest_ensure_response( [ |
| 344 | 350 | 'success' => 1, |
| 345 | 351 | 'status_code' => 200, |
| 346 | - 'message' => __( 'Successfully deleted all flows', Config::get( 'text_domain' ) ), | |
| 352 | + 'message' => __( 'Successfully deleted all flows', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 347 | 353 | 'data' => [ |
| 348 | 354 | 'items' => $flow_ids, |
| 349 | 355 | ], |
| 350 | 356 | ], 200 ); |
| @@ -359,15 +365,15 @@ | ||
| 359 | 365 | * |
| 360 | 366 | * @return JSON |
| 361 | 367 | */ |
| 362 | 368 | public function clone_item( $request ) { |
| 363 | - $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) ); | |
| 369 | + $response = Helpers::ens_verify_nonce( $request->get_header( 'x_wp_nonce' ), $this->identifier ); | |
| 364 | 370 | if ( $response instanceof WP_HTTP_Response ) { |
| 365 | 371 | return $response; |
| 366 | 372 | } |
| 367 | 373 | |
| 368 | 374 | $flow_id = (int) $request['flow_id']; |
| 369 | - $flow = new Flow( $flow_id ); | |
| 375 | + $flow = new Flow( $this->identifier, $flow_id ); | |
| 370 | 376 | |
| 371 | 377 | if ( !$flow->is_flow() ) { |
| 372 | 378 | return new WP_HTTP_Response( |
| 373 | 379 | [ |
| @@ -372,9 +378,9 @@ | ||
| 372 | 378 | return new WP_HTTP_Response( |
| 373 | 379 | [ |
| 374 | 380 | 'success' => 0, |
| 375 | 381 | 'status_code' => 422, |
| 376 | - 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ), | |
| 382 | + 'message' => __( 'Invalid flow id.', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 377 | 383 | 'data' => [], |
| 378 | 384 | ], |
| 379 | 385 | 422 |
| 380 | 386 | ); |
| @@ -386,9 +392,9 @@ | ||
| 386 | 392 | |
| 387 | 393 | $response = [ |
| 388 | 394 | 'success' => 1, |
| 389 | 395 | 'status_code' => 200, |
| 390 | - 'message' => __( 'Successfully cloned requested flow', Config::get( 'text_domain' ) ), | |
| 396 | + 'message' => __( 'Successfully cloned requested flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 391 | 397 | 'data' => $item, |
| 392 | 398 | ]; |
| 393 | 399 | |
| 394 | 400 | return rest_ensure_response( $response, 200 ); |
| @@ -404,9 +410,9 @@ | ||
| 404 | 410 | * |
| 405 | 411 | * @return WP_REST_Response |
| 406 | 412 | */ |
| 407 | 413 | public function save_flow( $request, $id = 0 ) { |
| 408 | - $flow = new Flow( $id ); | |
| 414 | + $flow = new Flow( $this->identifier, $id ); | |
| 409 | 415 | $request_body = $request->get_body(); |
| 410 | 416 | $data = !is_null( $request_body ) ? json_decode( $request_body, true ) : []; |
| 411 | 417 | |
| 412 | 418 | // Check if the input data is empty |
| @@ -413,9 +419,9 @@ | ||
| 413 | 419 | if ( empty( $data ) ) { |
| 414 | 420 | return rest_ensure_response( [ |
| 415 | 421 | 'status_code' => 400, |
| 416 | 422 | 'success' => 0, |
| 417 | - 'message' => __( 'No data provided to save the flow', Config::get( 'text_domain' ) ), | |
| 423 | + 'message' => __( 'No data provided to save the flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 418 | 424 | ], 400 ); |
| 419 | 425 | } |
| 420 | 426 | |
| 421 | 427 | $name = !empty( $data['name'] ) ? sanitize_text_field( $data['name'] ) : $flow->get_name(); |
| @@ -420,9 +426,9 @@ | ||
| 420 | 426 | |
| 421 | 427 | $name = !empty( $data['name'] ) ? sanitize_text_field( $data['name'] ) : $flow->get_name(); |
| 422 | 428 | $trigger = !empty( $data['trigger'] ) ? sanitize_text_field( $data['trigger'] ) : $flow->get_trigger(); |
| 423 | 429 | |
| 424 | - $flow_config = !empty( $data['flow_config'] ) ? ens_sanitize_recursive( $data['flow_config'] ) : $flow->get_flow_config(); | |
| 430 | + $flow_config = !empty( $data['flow_config'] ) ? Helpers::ens_sanitize_recursive( $data['flow_config'] ) : $flow->get_flow_config(); | |
| 425 | 431 | $status = ( isset( $data['status'] ) && !empty( $data['status'] ) ) ? sanitize_text_field( strtolower( $data['status'] ) ) : ( $flow->get_status() ? $flow->get_status() : 'draft' ); |
| 426 | 432 | |
| 427 | 433 | $action = $id ? 'updated' : 'created'; |
| 428 | 434 | |
| @@ -430,9 +436,9 @@ | ||
| 430 | 436 | if ( empty( $name ) || empty( $trigger ) ) { |
| 431 | 437 | return rest_ensure_response( [ |
| 432 | 438 | 'status_code' => 400, |
| 433 | 439 | 'success' => 0, |
| 434 | - 'message' => __( 'Flow name & trigger is required', Config::get( 'text_domain' ) ), | |
| 440 | + 'message' => __( 'Flow name & trigger is required', Helpers::get_config_data( $this->identifier,'text_domain' ) ), | |
| 435 | 441 | ], 400 ); |
| 436 | 442 | } |
| 437 | 443 | |
| 438 | 444 | // preparing flow data. |
| @@ -452,9 +458,9 @@ | ||
| 452 | 458 | $response = [ |
| 453 | 459 | 'status_code' => 200, |
| 454 | 460 | 'success' => 1, |
| 455 | 461 | // translators: %s is the name of the action |
| 456 | - 'message' => esc_html( sprintf( __( 'Successfully %s notification flow', Config::get( 'text_domain' ) ), $action ) ), | |
| 462 | + 'message' => esc_html( sprintf( __( 'Successfully %s notification flow', Helpers::get_config_data( $this->identifier,'text_domain' ) ), $action ) ), | |
| 457 | 463 | 'data' => $item, |
| 458 | 464 | ]; |
| 459 | 465 | |
| 460 | 466 | return rest_ensure_response( $response, 200 ); |
| @@ -469,9 +475,9 @@ | ||
| 469 | 475 | * |
| 470 | 476 | * @return array |
| 471 | 477 | */ |
| 472 | 478 | public function prepare_item( $flow_id ) { |
| 473 | - $flow = new Flow( $flow_id ); | |
| 479 | + $flow = new Flow( $this->identifier, $flow_id ); | |
| 474 | 480 | $data = [ |
| 475 | 481 | 'id' => $flow->get_id(), |
| 476 | 482 | 'name' => $flow->get_name(), |
| 477 | 483 | 'trigger' => $flow->get_trigger(), |
| @@ -478,7 +484,7 @@ | ||
| 478 | 484 | 'flow_config' => empty( $flow->get_flow_config() ) ? [] : $flow->get_flow_config(), |
| 479 | 485 | 'status' => $flow->get_status(), |
| 480 | 486 | ]; |
| 481 | 487 | |
| 482 | - return apply_filters( 'ens_flow_prepare_item', $data, $flow_id ); | |
| 488 | + return apply_filters( Helpers::get_hook_name( $this->identifier, 'ens_flow_prepare_item' ), $data, $flow_id ); | |
| 483 | 489 | } |
| 484 | 490 | } |