| 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\Db_Queries\Exceptions\Sql_Exception; |
| 13 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 14 |
use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 15 |
use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 16 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 17 |
use JFB_Components\Module\Base_Module_After_Install_It; |
| 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\Actions_V2\Register_User\Register_User_Action; |
| 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\Form_Record\Inner_Module; |
| 29 |
use JFB_Modules\Verification\Rest_Api\Endpoints; |
| 30 |
use JFB_Modules\Webhook; |
| 31 |
|
| 32 |
class Module implements |
| 33 |
Base_Module_It, |
| 34 |
Base_Module_Url_It, |
| 35 |
Base_Module_Handle_It, |
| 36 |
Base_Module_After_Install_It { |
| 37 |
|
| 38 |
use Base_Module_Dir_Trait; |
| 39 |
use Base_Module_Handle_Trait; |
| 40 |
use Base_Module_Url_Trait; |
| 41 |
|
| 42 |
const WAITING = 'verification_pending'; |
| 43 |
const EXPIRED = 'verification_expired'; |
| 44 |
const SUCCESS = 'verified'; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var Inner_Module |
| 48 |
*/ |
| 49 |
private $record; |
| 50 |
|
| 51 |
public function rep_item_id() { |
| 52 |
return 'verification'; |
| 53 |
} |
| 54 |
|
| 55 |
public function condition(): bool { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @throws Repository_Exception |
| 61 |
*/ |
| 62 |
public function on_install() { |
| 63 |
$this->record = new Inner_Module(); |
| 64 |
|
| 65 |
/** @var \JFB_Modules\Jobs\Module $jobs */ |
| 66 |
$jobs = jet_form_builder()->module( 'jobs' ); |
| 67 |
$jobs->install( new Jobs\Verify_Manually() ); |
| 68 |
|
| 69 |
/** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 70 |
$rest_api = jet_form_builder()->module( 'rest-api' ); |
| 71 |
$rest_api->get_controller()->install( new Endpoints\Verify_Manually() ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @throws Repository_Exception |
| 76 |
*/ |
| 77 |
public function on_uninstall() { |
| 78 |
unset( $this->record ); |
| 79 |
|
| 80 |
/** @var \JFB_Modules\Jobs\Module $jobs */ |
| 81 |
$jobs = jet_form_builder()->module( 'jobs' ); |
| 82 |
$jobs->uninstall( new Jobs\Verify_Manually() ); |
| 83 |
|
| 84 |
/** @var \JFB_Modules\Rest_Api\Module $rest_api */ |
| 85 |
$rest_api = jet_form_builder()->module( 'rest-api' ); |
| 86 |
$rest_api->get_controller()->uninstall( new Endpoints\Verify_Manually() ); |
| 87 |
} |
| 88 |
|
| 89 |
public function init_hooks() { |
| 90 |
add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ), 0 ); |
| 91 |
add_action( 'jet-form-builder/actions/register', array( $this, 'actions_register' ) ); |
| 92 |
/** |
| 93 |
* It runs by `webhook` module |
| 94 |
* |
| 95 |
* @see \JFB_Modules\Webhook\Module::confirm |
| 96 |
*/ |
| 97 |
add_action( 'jet-form-builder/webhook/verification', array( $this, 'on_verification' ) ); |
| 98 |
add_action( 'jet-form-builder/before-do-action/register_user', array( $this, 'register_user_before' ), 10, 2 ); |
| 99 |
|
| 100 |
add_filter( 'jet-form-builder/event-types', array( $this, 'events_register' ) ); |
| 101 |
add_filter( |
| 102 |
'jet-form-builder/default-process-event/executors', |
| 103 |
array( $this, 'add_executor_to_default_process' ) |
| 104 |
); |
| 105 |
|
| 106 |
$this->record->init_hooks(); |
| 107 |
} |
| 108 |
|
| 109 |
public function remove_hooks() { |
| 110 |
remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ), 0 ); |
| 111 |
remove_action( 'jet-form-builder/actions/register', array( $this, 'actions_register' ) ); |
| 112 |
remove_action( 'jet-form-builder/webhook/verification', array( $this, 'on_verification' ) ); |
| 113 |
remove_action( |
| 114 |
'jet-form-builder/before-do-action/register_user', |
| 115 |
array( $this, 'register_user_before' ) |
| 116 |
); |
| 117 |
remove_filter( 'jet-form-builder/event-types', array( $this, 'events_register' ) ); |
| 118 |
remove_filter( |
| 119 |
'jet-form-builder/default-process-event/executors', |
| 120 |
array( $this, 'add_executor_to_default_process' ) |
| 121 |
); |
| 122 |
|
| 123 |
$this->record->remove_hooks(); |
| 124 |
} |
| 125 |
|
| 126 |
public function enqueue_editor_assets() { |
| 127 |
$script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' ); |
| 128 |
|
| 129 |
wp_enqueue_script( |
| 130 |
$this->get_handle(), |
| 131 |
$this->get_url( 'assets/build/editor.js' ), |
| 132 |
$script_asset['dependencies'], |
| 133 |
$script_asset['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 $action |
| 165 |
*/ |
| 166 |
public function register_user_before( Register_User_Action $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 |
|