PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.0
JetFormBuilder — Dynamic Blocks Form Builder v3.3.0
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
253 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 wp_enqueue_script(
130 $this->get_handle(),
131 $this->get_url( 'assets/build/js/editor.js' ),
132 array(),
133 jet_form_builder()->get_version(),
134 true
135 );
136 }
137
138 public function actions_register( Manager $manager ) {
139 $manager->register_action_type( new Verification() );
140 }
141
142 public function events_register( array $types ): array {
143 array_push(
144 $types,
145 new Events\Verification_Success\Event(),
146 new Events\Verification_Failed\Event()
147 );
148
149 return $types;
150 }
151
152 public function add_executor_to_default_process( array $executors ): array {
153 return array_merge(
154 array(
155 new Events\Default_Process\Executor(),
156 ),
157 $executors
158 );
159 }
160
161 /**
162 * Generate unique token, if it used
163 *
164 * @param Register_User $action
165 */
166 public function register_user_before( Register_User $action ) {
167 $fields_map = array_values( $action->settings['fields_map'] ?? array() );
168
169 if (
170 ! in_array( Verification::TOKEN, $fields_map, true ) ||
171 jet_fb_context()->has_field( Verification::TOKEN )
172 ) {
173 return;
174 }
175
176 jet_fb_context()->set_field_type( 'text-field', Verification::TOKEN );
177 jet_fb_context()->update_request( Csrf_Tools::generate(), Verification::TOKEN );
178 jet_fb_context()->make_secure( Verification::TOKEN );
179 }
180
181 public function on_verification( Webhook\Module $module ) {
182 try {
183 $this->do_verification( $module );
184 } catch ( Handler_Exception $exception ) {
185 $module->redirect(
186 $this->get_redirect_url(
187 array(
188 'status' => $exception->getMessage(),
189 )
190 )
191 );
192 }
193
194 $module->redirect(
195 $this->get_redirect_url(
196 array(
197 'status' => $module->is_verified() ? 'success' : 'failed',
198 )
199 )
200 );
201 }
202
203 /**
204 * @param Webhook\Module $module
205 *
206 * @throws Action_Exception
207 * @throws Query_Builder_Exception
208 * @throws Sql_Exception
209 */
210 public function do_verification( Webhook\Module $module ) {
211 $record = Webhook\Form_Record\Db\Views\Record_By_Token_View::findOne(
212 array(
213 'token_id' => $module->get_token_id(),
214 )
215 )->query()->query_one();
216
217 // setup actions
218 jet_fb_action_handler()->set_form_id( $record['form_id'] );
219
220 // setup referrer for redirect actions
221 jet_fb_handler()->set_referrer( $record['referrer'] );
222
223 // setup fields & request for them
224 Tools::apply_context( $record );
225
226 $record_id = $record['id'] ?? 0;
227
228 jet_fb_context()->set_field_type( 'text-field', Verification::TOKEN );
229 jet_fb_context()->update_request( $module->get_token(), Verification::TOKEN );
230 jet_fb_context()->make_secure( Verification::TOKEN );
231
232 try {
233 jet_fb_events()->execute(
234 $module->is_verified()
235 ? Events\Verification_Success\Event::class
236 : Events\Verification_Failed\Event::class
237 );
238 } finally {
239 Tools::update_record( $record_id );
240 }
241 }
242
243 public function get_redirect_url( $args ): string {
244 $redirect = jet_fb_action_handler()->response_data['redirect'] ?? '';
245
246 if ( ! $redirect ) {
247 $redirect = jet_fb_handler()->get_referrer();
248 }
249
250 return add_query_arg( $args, $redirect );
251 }
252 }
253