PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.11
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.11
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / background-process.php

background-process.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.11, at inc/background-process.php

154 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms Background Process.
4 *
5 * @package sureforms.
6 * @since 0.0.3
7 */
8
9 namespace SRFM\Inc;
10
11 use SRFM\Inc\Traits\Get_Instance;
12 use SRFM\Inc\Helper;
13 use WP_REST_Server;
14 use WP_Error;
15 use WP_REST_Request;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Sureforms Background Process Class.
23 *
24 * @since 0.0.3
25 */
26 class Background_Process {
27 use Get_Instance;
28
29 /**
30 * Namespace.
31 *
32 * @var string
33 */
34 protected $namespace = 'sureforms/v1';
35 /**
36 * Submission Id.
37 *
38 * @var int
39 */
40 protected $submission_id = 0;
41 /**
42 * Form Id.
43 *
44 * @var int
45 */
46 protected $form_id = 0;
47 /**
48 * Submission Data.
49 *
50 * @var array<mixed>
51 */
52 protected $submission_data = [];
53
54 /**
55 * Constructor
56 *
57 * @since 0.0.3
58 */
59 public function __construct() {
60 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
61 }
62
63 /**
64 * Add custom API Route for 'After Submission' background process.
65 *
66 * @since 0.0.3
67 * @return void
68 */
69 public function register_custom_endpoint() {
70 register_rest_route(
71 $this->namespace,
72 '/after-submission/(?P<submission_id>[0-9]+)',
73 [
74 'methods' => WP_REST_Server::READABLE,
75 'callback' => [ $this, 'handle_after_submission' ],
76 'permission_callback' => '__return_true',
77 'args' => [
78 'submission_id' => [
79 'required' => true,
80 'validate_callback' => function( $param, $request, $key ) {
81 return is_integer( Helper::get_integer_value( $param ) ) && 0 < $param;
82 },
83 ],
84 ],
85 ]
86 );
87 }
88
89 /**
90 * Handle 'After Submission' background process
91 *
92 * @param \WP_REST_Request $request Request object or array containing form data.
93 * @since 0.0.3
94 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
95 */
96 public function handle_after_submission( $request ) {
97
98 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
99
100 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
101 return new \WP_Error(
102 'rest_nonce_failed',
103 __( 'Nonce verification failed.', 'sureforms' ),
104 [ 'status' => 403 ]
105 );
106 }
107
108 $this->submission_id = Helper::get_integer_value( $request->get_param( 'submission_id' ) );
109
110 if ( ! ( 0 < $this->submission_id ) ) {
111 return new \WP_Error(
112 'incomplete_request',
113 __( 'Submission id missing.', 'sureforms' ),
114 [ 'status' => 403 ]
115 );
116 }
117
118 $this->form_id = Helper::get_integer_value( get_post_meta( $this->submission_id, '_srfm_entry_form_id', true ) );
119 $this->submission_data = Helper::get_array_value( get_post_meta( $this->submission_id, 'srfm_entry_meta', true ) );
120
121 if ( ! $this->trigger_after_submission_process() ) {
122 return new \WP_Error(
123 'process_failed',
124 __( 'Something went wrong. We haved logged the error for further investigation', 'sureforms' ),
125 [ 'status' => 403 ]
126 );
127 };
128
129 return new \WP_REST_Response( [] );
130 }
131
132 /**
133 * Handle 'After Submission' background process
134 *
135 * @since 0.0.3
136 * @return bool
137 */
138 public function trigger_after_submission_process() {
139 if ( ! is_integer( $this->form_id ) || ! is_array( $this->submission_data ) ) {
140 return false;
141 }
142 $form_data = $this->submission_data;
143 $form_data['form_id'] = $this->form_id;
144 $form_data['submission_id'] = $this->form_id;
145 /**
146 * Hook for enabling background processes.
147 *
148 * @param array $form_data form data related to submission.
149 */
150 do_action( 'srfm_after_submission_process', $form_data );
151 return true;
152 }
153 }
154