verify-manually.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Verification\Jobs; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die(); |
| 9 | } |
| 10 | |
| 11 | use Jet_Form_Builder\Actions\Types\Base; |
| 12 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 13 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 14 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 15 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 16 | use JFB_Modules\Jobs\Async_Job; |
| 17 | use JFB_Modules\Jobs; |
| 18 | use JFB_Modules\Webhook; |
| 19 | use JFB_Modules\Verification; |
| 20 | use JFB_Modules\Security; |
| 21 | use JFB_Modules\Verification\Events; |
| 22 | use JFB_Modules\Webhook\Form_Record\Db\Views\Token_By_Record_View; |
| 23 | |
| 24 | class Verify_Manually extends Async_Job implements |
| 25 | Jobs\Interfaces\Self_Execution_Job_It, |
| 26 | Jobs\Interfaces\Job_Logger_It { |
| 27 | |
| 28 | use Jobs\Traits\Self_Execution_Job_Trait; |
| 29 | use Jobs\Traits\Job_Logger_Trait; |
| 30 | |
| 31 | private $real_user_id = 0; |
| 32 | |
| 33 | public function __construct() { |
| 34 | $this->set_hook( 'verification/verify' ); |
| 35 | } |
| 36 | |
| 37 | public function init_hooks() { |
| 38 | add_action( $this->get_hook(), array( $this, 'execute' ), 10, 2 ); |
| 39 | } |
| 40 | |
| 41 | public function remove_hooks() { |
| 42 | remove_action( $this->get_hook(), array( $this, 'execute' ) ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param $record_id |
| 47 | * @param $user_id |
| 48 | * |
| 49 | * @throws Repository_Exception |
| 50 | */ |
| 51 | public function execute( $record_id, $user_id ) { |
| 52 | $this->set_args( array( $record_id, $user_id ) ); |
| 53 | /** @var Webhook\Module $webhooks */ |
| 54 | $webhooks = jet_form_builder()->module( 'webhook' ); |
| 55 | /** @var Verification\Module $verification */ |
| 56 | $verification = jet_form_builder()->module( 'verification' ); |
| 57 | |
| 58 | try { |
| 59 | $token_row = Token_By_Record_View::findOne( |
| 60 | array( |
| 61 | 'record_id' => $record_id, |
| 62 | ) |
| 63 | )->query()->query_one(); |
| 64 | } catch ( Query_Builder_Exception $exception ) { |
| 65 | $this->log( __( 'Undefined webhook by record-ID', 'jet-form-builder' ) ); |
| 66 | |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Triggered by $webhooks->confirm(); |
| 72 | * |
| 73 | * @see \JFB_Modules\Webhook\Module::confirm |
| 74 | */ |
| 75 | add_action( 'jet-form-builder/webhook/verification', array( $this, 'execute_actions' ) ); |
| 76 | add_action( 'jet-form-builder/before-do-action/register_user', array( $this, 'modify_register_user' ) ); |
| 77 | |
| 78 | // Disable base verification process |
| 79 | remove_action( 'jet-form-builder/webhook/verification', array( $verification, 'on_verification' ) ); |
| 80 | |
| 81 | $this->set_user( $user_id ); |
| 82 | |
| 83 | $webhooks->set_token_id( (int) $token_row['id'] ); |
| 84 | $webhooks->set_token( Security\Csrf\Csrf_Tools::generate() ); |
| 85 | $webhooks->set_check_hash( false ); |
| 86 | $webhooks->confirm(); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * @param Webhook\Module $webhooks |
| 92 | * |
| 93 | * @throws Repository_Exception |
| 94 | */ |
| 95 | public function execute_actions( Webhook\Module $webhooks ) { |
| 96 | global $wpdb; |
| 97 | /** @var Verification\Module $verification */ |
| 98 | $verification = jet_form_builder()->module( 'verification' ); |
| 99 | |
| 100 | try { |
| 101 | $verification->do_verification( $webhooks ); |
| 102 | } catch ( Sql_Exception $exception ) { |
| 103 | $this->log( |
| 104 | sprintf( |
| 105 | /* translators: %s - message from the exception */ |
| 106 | __( 'Failed update from-record: %1$s, %2$s', 'jet-form-builder' ), |
| 107 | $exception->getMessage(), |
| 108 | $wpdb->last_error |
| 109 | ) |
| 110 | ); |
| 111 | } catch ( Action_Exception $exception ) { |
| 112 | $this->log( |
| 113 | /* translators: %d - message from the exception */ |
| 114 | sprintf( __( 'Failed actions execution: %s', 'jet-form-builder' ), $exception->getMessage() ) |
| 115 | ); |
| 116 | } catch ( Query_Builder_Exception $exception ) { |
| 117 | $this->log( |
| 118 | /* translators: %d - id of the form record */ |
| 119 | sprintf( __( 'Undefined record by webhook-ID: %d', 'jet-form-builder' ), $webhooks->get_token_id() ) |
| 120 | ); |
| 121 | } finally { |
| 122 | $this->reset_user(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public function modify_register_user( Base $action ) { |
| 127 | $action->settings['allow_register'] = true; |
| 128 | $action->settings['role_can_register'] = wp_get_current_user()->roles[0]; |
| 129 | $action->settings['log_in'] = false; |
| 130 | } |
| 131 | |
| 132 | protected function set_user( $user_id ) { |
| 133 | $this->set_real_user_id( get_current_user_id() ); |
| 134 | |
| 135 | wp_set_current_user( $user_id ); |
| 136 | } |
| 137 | |
| 138 | protected function reset_user() { |
| 139 | wp_set_current_user( $this->real_user_id ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param int $real_user_id |
| 144 | */ |
| 145 | protected function set_real_user_id( int $real_user_id ) { |
| 146 | $this->real_user_id = $real_user_id; |
| 147 | } |
| 148 | } |
| 149 |