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