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