PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.0.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.0.2
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 1.0.2, at inc/background-process.php

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