background-process.php
| 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 | |
| 96 | $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); |
| 97 | |
| 98 | if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { |
| 99 | return new \WP_Error( |
| 100 | 'rest_nonce_failed', |
| 101 | __( 'Nonce verification failed.', 'sureforms' ), |
| 102 | [ 'status' => 403 ] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | $this->submission_id = Helper::get_integer_value( $request->get_param( 'submission_id' ) ); |
| 107 | |
| 108 | if ( ! ( 0 < $this->submission_id ) ) { |
| 109 | return new \WP_Error( |
| 110 | 'incomplete_request', |
| 111 | __( 'Submission id missing.', 'sureforms' ), |
| 112 | [ 'status' => 403 ] |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | // Get the entries data for further processing, related to webhooks. |
| 117 | $entry_data = Entries::get( $this->submission_id ); |
| 118 | $this->form_id = Helper::get_integer_value( $entry_data['form_id'] ); |
| 119 | $this->submission_data = Helper::get_array_value( $entry_data['form_data'] ); |
| 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->submission_id; // Refers to the entry 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 |