actions
2 years ago
assets
2 years ago
events
2 years ago
form-record
2 years ago
module.php
2 years ago
module.php
222 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Verification; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | use Jet_Form_Builder\Actions\Manager; |
| 12 | use Jet_Form_Builder\Actions\Types\Register_User; |
| 13 | use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Base_Meta_Container; |
| 14 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 15 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 16 | use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 17 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 18 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 19 | use JFB_Components\Module\Base_Module_Handle_It; |
| 20 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 21 | use JFB_Components\Module\Base_Module_It; |
| 22 | use JFB_Components\Module\Base_Module_Url_It; |
| 23 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 24 | use JFB_Modules\Form_Record\Tools; |
| 25 | use JFB_Modules\Security\Csrf\Csrf_Tools; |
| 26 | use JFB_Modules\Verification\Actions\Verification; |
| 27 | use JFB_Modules\Verification\Events; |
| 28 | use JFB_Modules\Verification\Form_Record\Admin\Meta_Boxes\Verification_Box; |
| 29 | use JFB_Modules\Webhook; |
| 30 | |
| 31 | class Module implements Base_Module_It, Base_Module_Url_It, Base_Module_Handle_It { |
| 32 | |
| 33 | use Base_Module_Dir_Trait; |
| 34 | use Base_Module_Handle_Trait; |
| 35 | use Base_Module_Url_Trait; |
| 36 | |
| 37 | public function rep_item_id() { |
| 38 | return 'verification'; |
| 39 | } |
| 40 | |
| 41 | public function condition(): bool { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | public function init_hooks() { |
| 46 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) ); |
| 47 | add_action( 'jet-form-builder/actions/register', array( $this, 'actions_register' ) ); |
| 48 | /** |
| 49 | * It runs by `webhook` module |
| 50 | * |
| 51 | * @see \JFB_Modules\Webhook\Module::try_to_catch |
| 52 | */ |
| 53 | add_action( 'jet-form-builder/webhook/verification', array( $this, 'on_verification' ) ); |
| 54 | add_action( 'jet-form-builder/before-do-action/register_user', array( $this, 'register_user_before' ), 10, 2 ); |
| 55 | |
| 56 | add_filter( 'jet-form-builder/event-types', array( $this, 'events_register' ) ); |
| 57 | add_filter( |
| 58 | 'jet-form-builder/default-process-event/executors', |
| 59 | array( $this, 'add_executor_to_default_process' ) |
| 60 | ); |
| 61 | add_filter( |
| 62 | 'jet-form-builder/page-containers/jfb-records-single', |
| 63 | array( $this, 'add_box_to_single_record' ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | public function remove_hooks() { |
| 68 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) ); |
| 69 | remove_action( 'jet-form-builder/actions/register', array( $this, 'actions_register' ) ); |
| 70 | remove_action( 'jet-form-builder/webhook/verification', array( $this, 'on_verification' ) ); |
| 71 | remove_action( |
| 72 | 'jet-form-builder/before-do-action/register_user', |
| 73 | array( $this, 'register_user_before' ) |
| 74 | ); |
| 75 | remove_filter( 'jet-form-builder/event-types', array( $this, 'events_register' ) ); |
| 76 | remove_filter( |
| 77 | 'jet-form-builder/default-process-event/executors', |
| 78 | array( $this, 'add_executor_to_default_process' ) |
| 79 | ); |
| 80 | remove_filter( |
| 81 | 'jet-form-builder/page-containers/jfb-records-single', |
| 82 | array( $this, 'add_box_to_single_record' ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | public function enqueue_editor_assets() { |
| 87 | wp_enqueue_script( |
| 88 | $this->get_handle(), |
| 89 | $this->get_url( 'assets/build/js/editor.js' ), |
| 90 | array(), |
| 91 | jet_form_builder()->get_version(), |
| 92 | true |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | public function actions_register( Manager $manager ) { |
| 97 | $manager->register_action_type( new Verification() ); |
| 98 | } |
| 99 | |
| 100 | public function events_register( array $types ): array { |
| 101 | array_push( |
| 102 | $types, |
| 103 | new Events\Verification_Success\Event(), |
| 104 | new Events\Verification_Failed\Event() |
| 105 | ); |
| 106 | |
| 107 | return $types; |
| 108 | } |
| 109 | |
| 110 | public function add_executor_to_default_process( array $executors ): array { |
| 111 | return array_merge( |
| 112 | array( |
| 113 | new Events\Default_Process\Executor(), |
| 114 | ), |
| 115 | $executors |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param Base_Meta_Container[] $containers |
| 121 | * |
| 122 | * @return array |
| 123 | */ |
| 124 | public function add_box_to_single_record( array $containers ): array { |
| 125 | $containers[1]->add_meta_box( new Verification_Box() ); |
| 126 | |
| 127 | return $containers; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Generate unique token, if it used |
| 132 | * |
| 133 | * @param Register_User $action |
| 134 | */ |
| 135 | public function register_user_before( Register_User $action ) { |
| 136 | $fields_map = array_values( $action->settings['fields_map'] ?? array() ); |
| 137 | |
| 138 | if ( |
| 139 | ! in_array( Verification::TOKEN, $fields_map, true ) || |
| 140 | jet_fb_context()->has_field( Verification::TOKEN ) |
| 141 | ) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | jet_fb_context()->set_field_type( 'text-field', Verification::TOKEN ); |
| 146 | jet_fb_context()->update_request( Csrf_Tools::generate(), Verification::TOKEN ); |
| 147 | jet_fb_context()->make_secure( Verification::TOKEN ); |
| 148 | } |
| 149 | |
| 150 | public function on_verification( Webhook\Module $module ) { |
| 151 | try { |
| 152 | $this->do_verification( $module ); |
| 153 | } catch ( Handler_Exception $exception ) { |
| 154 | $module->redirect( |
| 155 | $this->get_redirect_url( |
| 156 | array( |
| 157 | 'status' => $exception->getMessage(), |
| 158 | ) |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | $module->redirect( |
| 164 | $this->get_redirect_url( |
| 165 | array( |
| 166 | 'status' => $module->is_verified() ? 'success' : 'failed', |
| 167 | ) |
| 168 | ) |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param Webhook\Module $module |
| 174 | * |
| 175 | * @throws Action_Exception |
| 176 | * @throws Query_Builder_Exception |
| 177 | * @throws Sql_Exception |
| 178 | */ |
| 179 | public function do_verification( Webhook\Module $module ) { |
| 180 | $record = Webhook\Db\Views\Record_By_Token_View::findOne( |
| 181 | array( |
| 182 | 'token_id' => $module->get_token_id(), |
| 183 | ) |
| 184 | )->query()->query_one(); |
| 185 | |
| 186 | // setup actions |
| 187 | jet_fb_action_handler()->set_form_id( $record['form_id'] ); |
| 188 | |
| 189 | // setup referrer for redirect actions |
| 190 | jet_fb_handler()->set_referrer( $record['referrer'] ); |
| 191 | |
| 192 | // setup fields & request for them |
| 193 | Tools::apply_context( $record ); |
| 194 | |
| 195 | $record_id = $record['id'] ?? 0; |
| 196 | |
| 197 | jet_fb_context()->set_field_type( 'text-field', Verification::TOKEN ); |
| 198 | jet_fb_context()->update_request( $module->get_token(), Verification::TOKEN ); |
| 199 | jet_fb_context()->make_secure( Verification::TOKEN ); |
| 200 | |
| 201 | try { |
| 202 | jet_fb_events()->execute( |
| 203 | $module->is_verified() |
| 204 | ? Events\Verification_Success\Event::class |
| 205 | : Events\Verification_Failed\Event::class |
| 206 | ); |
| 207 | } finally { |
| 208 | Tools::update_record( $record_id ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | public function get_redirect_url( $args ): string { |
| 213 | $redirect = jet_fb_action_handler()->response_data['redirect'] ?? ''; |
| 214 | |
| 215 | if ( ! $redirect ) { |
| 216 | $redirect = jet_fb_handler()->get_referrer(); |
| 217 | } |
| 218 | |
| 219 | return add_query_arg( $args, $redirect ); |
| 220 | } |
| 221 | } |
| 222 |