verification.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Verification\Actions; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | use Jet_Form_Builder\Actions\Action_Handler; |
| 12 | use Jet_Form_Builder\Actions\Actions_Tools; |
| 13 | use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event; |
| 14 | use Jet_Form_Builder\Actions\Events\Default_Required\Default_Required_Event; |
| 15 | use Jet_Form_Builder\Actions\Types\Base; |
| 16 | use Jet_Form_Builder\Db_Queries\Base_Db_Model; |
| 17 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 18 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 19 | use JFB_Modules\Webhook\Db\Models\Tokens_Model; |
| 20 | use JFB_Modules\Webhook\Db\Models\Tokens_To_Records_Model; |
| 21 | use JFB_Modules\Form_Record\Action_Types\Save_Record; |
| 22 | use JFB_Modules\Verification\Events\Verification_Success; |
| 23 | use JFB_Modules\Verification\Events\Verification_Failed; |
| 24 | use JFB_Modules\Verification\Module; |
| 25 | use JFB_Modules\Webhook; |
| 26 | use JFB_Modules\Security; |
| 27 | |
| 28 | class Verification extends Base { |
| 29 | |
| 30 | const TOKEN = '_jfb_verification_token'; |
| 31 | const TOKEN_ID = '_jfb_verification_token_id'; |
| 32 | const URL = '_jfb_verification_url'; |
| 33 | |
| 34 | public function get_id() { |
| 35 | return 'verification'; |
| 36 | } |
| 37 | |
| 38 | public function get_name() { |
| 39 | return __( 'Verification', 'jet-form-builder' ); |
| 40 | } |
| 41 | |
| 42 | public function unsupported_events(): array { |
| 43 | return array( |
| 44 | Default_Required_Event::class, |
| 45 | Verification_Success\Event::class, |
| 46 | Verification_Failed\Event::class, |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function get_required_events(): array { |
| 51 | return array( |
| 52 | Default_Process_Event::class, |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param array $request |
| 58 | * @param Action_Handler $handler |
| 59 | * |
| 60 | * @return void |
| 61 | * @throws Sql_Exception |
| 62 | */ |
| 63 | public function do_action( array $request, Action_Handler $handler ) { |
| 64 | // saving the request is required |
| 65 | Save_Record::add_hidden(); |
| 66 | |
| 67 | // set field-type to make this fields as objects, which extends Field_Data_Parser |
| 68 | jet_fb_context()->set_field_type( 'text-field', self::TOKEN ); |
| 69 | jet_fb_context()->set_field_type( 'text-field', self::URL ); |
| 70 | jet_fb_context()->set_field_type( 'number-field', self::TOKEN_ID ); |
| 71 | |
| 72 | // this fields shouldn't save as field in Form Record |
| 73 | jet_fb_context()->make_secure( self::TOKEN ); |
| 74 | jet_fb_context()->make_secure( self::URL ); |
| 75 | jet_fb_context()->make_secure( self::TOKEN_ID ); |
| 76 | |
| 77 | $lifespan = $this->settings['lifespan'] ?? ''; |
| 78 | |
| 79 | list( $id, $token ) = Tokens_Model::create_token( $this->get_id(), $lifespan ); |
| 80 | |
| 81 | // generate unique token for current request, which could be used in another actions |
| 82 | jet_fb_context()->update_request( $token, self::TOKEN ); |
| 83 | jet_fb_context()->update_request( $id, self::TOKEN_ID ); |
| 84 | jet_fb_context()->update_request( |
| 85 | add_query_arg( |
| 86 | array( |
| 87 | Webhook\Module::GET_TOKEN_ID => $id, |
| 88 | Webhook\Module::GET_TOKEN => jet_fb_context()->get_value( self::TOKEN ), |
| 89 | ), |
| 90 | jet_fb_handler()->refer |
| 91 | ), |
| 92 | self::URL |
| 93 | ); |
| 94 | |
| 95 | add_action( |
| 96 | 'jet-form-builder/form-handler/after-send', |
| 97 | array( $this, 'connect_record_or_delete_token' ), |
| 98 | 10, |
| 99 | 2 |
| 100 | ); |
| 101 | |
| 102 | $this->send_default_email(); |
| 103 | } |
| 104 | |
| 105 | public function send_default_email() { |
| 106 | if ( ! empty( $this->settings['custom_email'] ) || empty( $this->settings['mail_to'] ) ) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | /** @var Module $module */ |
| 111 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 112 | $module = jet_form_builder()->module( Module::class ); |
| 113 | |
| 114 | $generator = Actions_Tools::get_flow( |
| 115 | $module->get_dir( 'actions/send.email.flow.json' ) |
| 116 | ); |
| 117 | |
| 118 | /** |
| 119 | * @var Base $action |
| 120 | * @var array $props |
| 121 | */ |
| 122 | foreach ( $generator as list( $action, $props ) ) { |
| 123 | $action->settings['mail_to'] = 'form'; |
| 124 | $action->settings['from_field'] = $this->settings['mail_to']; |
| 125 | |
| 126 | jet_fb_action_handler()->add( $action, $props ); |
| 127 | /** |
| 128 | * Execute action immediately, because is should run on the same event. |
| 129 | * If it would be with ON.DEFAULT.STATE - we may not use it line of the code |
| 130 | * But we couldn't be sure, that current process runs with DEFAULT.STATE render-state |
| 131 | */ |
| 132 | jet_fb_action_handler()->process_single_action( $action ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Reset current action ID, because it was changed |
| 137 | * by `jet_fb_action_handler()->process_single_action` |
| 138 | */ |
| 139 | jet_fb_action_handler()->set_current_action( $this->_id ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param $handler |
| 144 | * @param $is_success |
| 145 | * |
| 146 | * @throws Sql_Exception |
| 147 | */ |
| 148 | public function connect_record_or_delete_token( $handler, $is_success ) { |
| 149 | $record_id = jet_fb_action_handler()->get_context( Save_Record::ID, 'id' ); |
| 150 | $token_id = jet_fb_context()->get_value( self::TOKEN_ID ); |
| 151 | |
| 152 | if ( ! $is_success ) { |
| 153 | ( new Tokens_Model() )->delete( array( 'id' => $token_id ) ); |
| 154 | |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | ( new Tokens_To_Records_Model() )->insert( |
| 159 | array( |
| 160 | 'token_id' => $token_id, |
| 161 | 'record_id' => $record_id, |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 |