| @@ -7,13 +7,11 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace SRFM\Inc; |
| 10 | 10 | |
| 11 | +use SRFM\Inc\Database\Tables\Entries; | |
| 11 | 12 | use SRFM\Inc\Traits\Get_Instance; |
| 12 | -use SRFM\Inc\Helper; | |
| 13 | 13 | use WP_REST_Server; |
| 14 | -use WP_Error; | |
| 15 | -use WP_REST_Request; | |
| 16 | 14 | |
| 17 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | 16 | exit; // Exit if accessed directly. |
| 19 | 17 | } |
| @@ -76,9 +74,9 @@ | ||
| 76 | 74 | 'permission_callback' => '__return_true', |
| 77 | 75 | 'args' => [ |
| 78 | 76 | 'submission_id' => [ |
| 79 | 77 | 'required' => true, |
| 80 | - 'validate_callback' => function( $param, $request, $key ) { | |
| 78 | + 'validate_callback' => static function( $param ) { | |
| 81 | 79 | return is_integer( Helper::get_integer_value( $param ) ) && 0 < $param; |
| 82 | 80 | }, |
| 83 | 81 | ], |
| 84 | 82 | ], |
| @@ -93,13 +91,24 @@ | ||
| 93 | 91 | * @since 0.0.3 |
| 94 | 92 | * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 95 | 93 | */ |
| 96 | 94 | public function handle_after_submission( $request ) { |
| 95 | + $this->submission_id = Helper::get_integer_value( $request->get_param( 'submission_id' ) ); | |
| 97 | 96 | |
| 98 | - $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); | |
| 97 | + $extras = $this->get_extras_data( $this->submission_id ); | |
| 98 | + $is_after_submission_process_triggered = $extras['is_after_submission_process_triggered'] ?? false; | |
| 99 | 99 | |
| 100 | - if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { | |
| 100 | + if ( $is_after_submission_process_triggered ) { | |
| 101 | 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( | |
| 102 | 111 | 'rest_nonce_failed', |
| 103 | 112 | __( 'Nonce verification failed.', 'sureforms' ), |
| 104 | 113 | [ 'status' => 403 ] |
| 105 | 114 | ); |
| @@ -104,10 +113,8 @@ | ||
| 104 | 113 | [ 'status' => 403 ] |
| 105 | 114 | ); |
| 106 | 115 | } |
| 107 | 116 | |
| 108 | - $this->submission_id = Helper::get_integer_value( $request->get_param( 'submission_id' ) ); | |
| 109 | - | |
| 110 | 117 | if ( ! ( 0 < $this->submission_id ) ) { |
| 111 | 118 | return new \WP_Error( |
| 112 | 119 | 'incomplete_request', |
| 113 | 120 | __( 'Submission id missing.', 'sureforms' ), |
| @@ -114,18 +121,20 @@ | ||
| 114 | 121 | [ 'status' => 403 ] |
| 115 | 122 | ); |
| 116 | 123 | } |
| 117 | 124 | |
| 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 ) ); | |
| 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'] ); | |
| 120 | 129 | |
| 121 | 130 | if ( ! $this->trigger_after_submission_process() ) { |
| 122 | 131 | return new \WP_Error( |
| 123 | 132 | 'process_failed', |
| 124 | - __( 'Something went wrong. We haved logged the error for further investigation', 'sureforms' ), | |
| 133 | + __( 'Something went wrong. We have logged the error for further investigation', 'sureforms' ), | |
| 125 | 134 | [ 'status' => 403 ] |
| 126 | 135 | ); |
| 127 | - }; | |
| 136 | + } | |
| 128 | 137 | |
| 129 | 138 | return new \WP_REST_Response( [] ); |
| 130 | 139 | } |
| 131 | 140 | |
| @@ -140,9 +149,9 @@ | ||
| 140 | 149 | return false; |
| 141 | 150 | } |
| 142 | 151 | $form_data = $this->submission_data; |
| 143 | 152 | $form_data['form_id'] = $this->form_id; |
| 144 | - $form_data['submission_id'] = $this->form_id; | |
| 153 | + $form_data['submission_id'] = $this->submission_id; // Refers to the entry ID. | |
| 145 | 154 | /** |
| 146 | 155 | * Hook for enabling background processes. |
| 147 | 156 | * |
| 148 | 157 | * @param array $form_data form data related to submission. |
| @@ -147,7 +156,37 @@ | ||
| 147 | 156 | * |
| 148 | 157 | * @param array $form_data form data related to submission. |
| 149 | 158 | */ |
| 150 | 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 | + | |
| 151 | 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'] ) ?? []; | |
| 152 | 191 | } |
| 153 | 192 | } |