PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.4
JetFormBuilder — Dynamic Blocks Form Builder v3.3.4
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / verification / module.php
jetformbuilder / modules / verification Last commit date
actions 2 years ago assets 2 years ago events 2 years ago form-record 2 years ago jobs 2 years ago rest-api 2 years ago module.php 2 years ago
module.php
255 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\Db_Queries\Exceptions\Sql_Exception;
14 use Jet_Form_Builder\Exceptions\Action_Exception;
15 use Jet_Form_Builder\Exceptions\Handler_Exception;
16 use Jet_Form_Builder\Exceptions\Query_Builder_Exception;
17 use Jet_Form_Builder\Exceptions\Repository_Exception;
18 use JFB_Components\Module\Base_Module_After_Install_It;
19 use JFB_Components\Module\Base_Module_Dir_Trait;
20 use JFB_Components\Module\Base_Module_Handle_It;
21 use JFB_Components\Module\Base_Module_Handle_Trait;
22 use JFB_Components\Module\Base_Module_It;
23 use JFB_Components\Module\Base_Module_Url_It;
24 use JFB_Components\Module\Base_Module_Url_Trait;
25 use JFB_Modules\Form_Record\Tools;
26 use JFB_Modules\Security\Csrf\Csrf_Tools;
27 use JFB_Modules\Verification\Actions\Verification;
28 use JFB_Modules\Verification\Events;
29 use JFB_Modules\Verification\Form_Record\Inner_Module;
30 use JFB_Modules\Verification\Jobs;
31 use JFB_Modules\Webhook;
32 use JFB_Modules\Verification\Rest_Api\Endpoints;
33
34 class Module implements
35 Base_Module_It,
36 Base_Module_Url_It,
37 Base_Module_Handle_It,
38 Base_Module_After_Install_It {
39
40 use Base_Module_Dir_Trait;
41 use Base_Module_Handle_Trait;
42 use Base_Module_Url_Trait;
43
44 const WAITING = 'verification_pending';
45 const EXPIRED = 'verification_expired';
46 const SUCCESS = 'verified';
47
48 /**
49 * @var Inner_Module
50 */
51 private $record;
52
53 public function rep_item_id() {
54 return 'verification';
55 }
56
57 public function condition(): bool {
58 return true;
59 }
60
61 /**
62 * @throws Repository_Exception
63 */
64 public function on_install() {
65 $this->record = new Inner_Module();
66
67 /** @var \JFB_Modules\Jobs\Module $jobs */
68 $jobs = jet_form_builder()->module( 'jobs' );
69 $jobs->install( new Jobs\Verify_Manually() );
70
71 /** @var \JFB_Modules\Rest_Api\Module $rest_api */
72 $rest_api = jet_form_builder()->module( 'rest-api' );
73 $rest_api->get_controller()->install( new Endpoints\Verify_Manually() );
74 }
75
76 /**
77 * @throws Repository_Exception
78 */
79 public function on_uninstall() {
80 unset( $this->record );
81
82 /** @var \JFB_Modules\Jobs\Module $jobs */
83 $jobs = jet_form_builder()->module( 'jobs' );
84 $jobs->uninstall( new Jobs\Verify_Manually() );
85
86 /** @var \JFB_Modules\Rest_Api\Module $rest_api */
87 $rest_api = jet_form_builder()->module( 'rest-api' );
88 $rest_api->get_controller()->uninstall( new Endpoints\Verify_Manually() );
89 }
90
91 public function init_hooks() {
92 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ), 0 );
93 add_action( 'jet-form-builder/actions/register', array( $this, 'actions_register' ) );
94 /**
95 * It runs by `webhook` module
96 *
97 * @see \JFB_Modules\Webhook\Module::confirm
98 */
99 add_action( 'jet-form-builder/webhook/verification', array( $this, 'on_verification' ) );
100 add_action( 'jet-form-builder/before-do-action/register_user', array( $this, 'register_user_before' ), 10, 2 );
101
102 add_filter( 'jet-form-builder/event-types', array( $this, 'events_register' ) );
103 add_filter(
104 'jet-form-builder/default-process-event/executors',
105 array( $this, 'add_executor_to_default_process' )
106 );
107
108 $this->record->init_hooks();
109 }
110
111 public function remove_hooks() {
112 remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ), 0 );
113 remove_action( 'jet-form-builder/actions/register', array( $this, 'actions_register' ) );
114 remove_action( 'jet-form-builder/webhook/verification', array( $this, 'on_verification' ) );
115 remove_action(
116 'jet-form-builder/before-do-action/register_user',
117 array( $this, 'register_user_before' )
118 );
119 remove_filter( 'jet-form-builder/event-types', array( $this, 'events_register' ) );
120 remove_filter(
121 'jet-form-builder/default-process-event/executors',
122 array( $this, 'add_executor_to_default_process' )
123 );
124
125 $this->record->remove_hooks();
126 }
127
128 public function enqueue_editor_assets() {
129 $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' );
130
131 wp_enqueue_script(
132 $this->get_handle(),
133 $this->get_url( 'assets/build/editor.js' ),
134 $script_asset['dependencies'],
135 $script_asset['version'],
136 true
137 );
138 }
139
140 public function actions_register( Manager $manager ) {
141 $manager->register_action_type( new Verification() );
142 }
143
144 public function events_register( array $types ): array {
145 array_push(
146 $types,
147 new Events\Verification_Success\Event(),
148 new Events\Verification_Failed\Event()
149 );
150
151 return $types;
152 }
153
154 public function add_executor_to_default_process( array $executors ): array {
155 return array_merge(
156 array(
157 new Events\Default_Process\Executor(),
158 ),
159 $executors
160 );
161 }
162
163 /**
164 * Generate unique token, if it used
165 *
166 * @param Register_User $action
167 */
168 public function register_user_before( Register_User $action ) {
169 $fields_map = array_values( $action->settings['fields_map'] ?? array() );
170
171 if (
172 ! in_array( Verification::TOKEN, $fields_map, true ) ||
173 jet_fb_context()->has_field( Verification::TOKEN )
174 ) {
175 return;
176 }
177
178 jet_fb_context()->set_field_type( 'text-field', Verification::TOKEN );
179 jet_fb_context()->update_request( Csrf_Tools::generate(), Verification::TOKEN );
180 jet_fb_context()->make_secure( Verification::TOKEN );
181 }
182
183 public function on_verification( Webhook\Module $module ) {
184 try {
185 $this->do_verification( $module );
186 } catch ( Handler_Exception $exception ) {
187 $module->redirect(
188 $this->get_redirect_url(
189 array(
190 'status' => $exception->getMessage(),
191 )
192 )
193 );
194 }
195
196 $module->redirect(
197 $this->get_redirect_url(
198 array(
199 'status' => $module->is_verified() ? 'success' : 'failed',
200 )
201 )
202 );
203 }
204
205 /**
206 * @param Webhook\Module $module
207 *
208 * @throws Action_Exception
209 * @throws Query_Builder_Exception
210 * @throws Sql_Exception
211 */
212 public function do_verification( Webhook\Module $module ) {
213 $record = Webhook\Form_Record\Db\Views\Record_By_Token_View::findOne(
214 array(
215 'token_id' => $module->get_token_id(),
216 )
217 )->query()->query_one();
218
219 // setup actions
220 jet_fb_action_handler()->set_form_id( $record['form_id'] );
221
222 // setup referrer for redirect actions
223 jet_fb_handler()->set_referrer( $record['referrer'] );
224
225 // setup fields & request for them
226 Tools::apply_context( $record );
227
228 $record_id = $record['id'] ?? 0;
229
230 jet_fb_context()->set_field_type( 'text-field', Verification::TOKEN );
231 jet_fb_context()->update_request( $module->get_token(), Verification::TOKEN );
232 jet_fb_context()->make_secure( Verification::TOKEN );
233
234 try {
235 jet_fb_events()->execute(
236 $module->is_verified()
237 ? Events\Verification_Success\Event::class
238 : Events\Verification_Failed\Event::class
239 );
240 } finally {
241 Tools::update_record( $record_id );
242 }
243 }
244
245 public function get_redirect_url( $args ): string {
246 $redirect = jet_fb_action_handler()->response_data['redirect'] ?? '';
247
248 if ( ! $redirect ) {
249 $redirect = jet_fb_handler()->get_referrer();
250 }
251
252 return add_query_arg( $args, $redirect );
253 }
254 }
255