| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Verification\Form_Record; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Base_Meta_Container; |
| 12 |
use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 13 |
use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 14 |
use Jet_Form_Builder\Db_Queries\Query_Conditions_Builder; |
| 15 |
use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 16 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 17 |
use Jet_Form_Builder\Form_Messages\Status_Info; |
| 18 |
use JFB_Components\Module\Base_Module_Handle_It; |
| 19 |
use JFB_Components\Module\Base_Module_Handle_Trait; |
| 20 |
use JFB_Components\Module\Base_Module_It; |
| 21 |
use JFB_Components\Rest_Api\Rest_Endpoint; |
| 22 |
use JFB_Modules\Verification\Actions\Verification; |
| 23 |
use JFB_Modules\Verification\Form_Record\Admin\Columns\Status_Column; |
| 24 |
use JFB_Modules\Verification\Form_Record\Admin\Meta_Boxes\Verification_Box; |
| 25 |
use JFB_Modules\Verification\Module; |
| 26 |
use JFB_Modules\Verification\Rest_Api\Endpoints\Verify_Manually; |
| 27 |
use JFB_Modules\Webhook\Db\Models\Tokens_Model; |
| 28 |
use JFB_Modules\Webhook\Form_Record\Db\Models\Tokens_To_Records_Model; |
| 29 |
use JFB_Modules\Webhook\Form_Record\Db\Views\Token_By_Record_View; |
| 30 |
|
| 31 |
final class Inner_Module implements Base_Module_It, Base_Module_Handle_It { |
| 32 |
|
| 33 |
use Base_Module_Handle_Trait; |
| 34 |
|
| 35 |
public function rep_item_id() { |
| 36 |
return 'verification-form-record'; |
| 37 |
} |
| 38 |
|
| 39 |
public function condition(): bool { |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
public function init_hooks() { |
| 44 |
add_filter( |
| 45 |
'jet-form-builder/page-containers/jfb-records-single', |
| 46 |
array( $this, 'add_box_to_single_record' ) |
| 47 |
); |
| 48 |
add_filter( |
| 49 |
'jet-form-builder/form-record/list', |
| 50 |
array( $this, 'modify_columns_for_records_page' ) |
| 51 |
); |
| 52 |
add_filter( |
| 53 |
'jet-form-builder/page-config/jfb-records', |
| 54 |
array( $this, 'add_actions_for_records_page' ) |
| 55 |
); |
| 56 |
add_filter( |
| 57 |
'jet-form-builder/page-config/jfb-records-single', |
| 58 |
array( $this, 'remove_verify_button_for_records_single_page' ) |
| 59 |
); |
| 60 |
add_action( |
| 61 |
'jet-fb/admin-pages/before-assets/jfb-records', |
| 62 |
array( $this, 'enqueue_assets_for_records' ) |
| 63 |
); |
| 64 |
add_action( |
| 65 |
'jet-fb/admin-pages/before-assets/jfb-records-single', |
| 66 |
array( $this, 'enqueue_assets_for_record_single' ) |
| 67 |
); |
| 68 |
add_action( |
| 69 |
'jet-form-builder/db/records/after-insert', |
| 70 |
array( $this, 'connect_record_or_delete_token' ), |
| 71 |
10, |
| 72 |
2 |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function remove_hooks() { |
| 77 |
remove_filter( |
| 78 |
'jet-form-builder/page-containers/jfb-records-single', |
| 79 |
array( $this, 'add_box_to_single_record' ) |
| 80 |
); |
| 81 |
remove_filter( |
| 82 |
'jet-form-builder/form-record/list', |
| 83 |
array( $this, 'modify_columns_for_records_page' ) |
| 84 |
); |
| 85 |
remove_filter( |
| 86 |
'jet-form-builder/page-config/jfb-records', |
| 87 |
array( $this, 'add_actions_for_records_page' ) |
| 88 |
); |
| 89 |
remove_filter( |
| 90 |
'jet-form-builder/page-config/jfb-records-single', |
| 91 |
array( $this, 'remove_verify_button_for_records_single_page' ) |
| 92 |
); |
| 93 |
remove_action( |
| 94 |
'jet-fb/admin-pages/before-assets/jfb-records', |
| 95 |
array( $this, 'enqueue_assets_for_records' ) |
| 96 |
); |
| 97 |
remove_action( |
| 98 |
'jet-fb/admin-pages/before-assets/jfb-records-single', |
| 99 |
array( $this, 'enqueue_assets_for_record_single' ) |
| 100 |
); |
| 101 |
remove_action( |
| 102 |
'jet-form-builder/db/records/after-insert', |
| 103 |
array( $this, 'connect_record_or_delete_token' ) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @param Base_Meta_Container[] $containers |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function add_box_to_single_record( array $containers ): array { |
| 113 |
$containers[1]->add_meta_box( new Verification_Box() ); |
| 114 |
|
| 115 |
return $containers; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @throws Repository_Exception |
| 120 |
*/ |
| 121 |
public function enqueue_assets_for_records() { |
| 122 |
/** @var Module $module */ |
| 123 |
$module = jet_form_builder()->module( 'verification' ); |
| 124 |
$script_asset = require_once $module->get_dir( 'assets/build/admin/form-records.asset.php' ); |
| 125 |
|
| 126 |
wp_enqueue_script( |
| 127 |
$this->get_handle(), |
| 128 |
$module->get_url( 'assets/build/admin/form-records.js' ), |
| 129 |
$script_asset['dependencies'], |
| 130 |
$script_asset['version'], |
| 131 |
true |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @throws Repository_Exception |
| 137 |
*/ |
| 138 |
public function enqueue_assets_for_record_single() { |
| 139 |
/** @var Module $module */ |
| 140 |
$module = jet_form_builder()->module( 'verification' ); |
| 141 |
$script_asset = require_once $module->get_dir( 'assets/build/admin/form-record-single.asset.php' ); |
| 142 |
|
| 143 |
wp_enqueue_script( |
| 144 |
$this->get_handle(), |
| 145 |
$module->get_url( 'assets/build/admin/form-record-single.js' ), |
| 146 |
$script_asset['dependencies'], |
| 147 |
$script_asset['version'], |
| 148 |
true |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
public function modify_columns_for_records_page( array $list ): array { |
| 153 |
$ids = array(); |
| 154 |
|
| 155 |
foreach ( $list as &$record ) { |
| 156 |
$ids[] = (int) $record['choose']['value']; |
| 157 |
} |
| 158 |
|
| 159 |
try { |
| 160 |
$tokens = Token_By_Record_View::find( |
| 161 |
array( |
| 162 |
array( |
| 163 |
'type' => Query_Conditions_Builder::TYPE_IN, |
| 164 |
'values' => array( 'record_id', $ids ), |
| 165 |
), |
| 166 |
) |
| 167 |
)->query()->query_all(); |
| 168 |
} catch ( Query_Builder_Exception $exception ) { |
| 169 |
return $list; |
| 170 |
} |
| 171 |
|
| 172 |
$sorted_tokens = array(); |
| 173 |
$labels = $this->get_verification_labels(); |
| 174 |
|
| 175 |
foreach ( $tokens as &$token_row ) { |
| 176 |
$sorted_tokens[ $token_row['main']['record_id'] ] = $token_row; |
| 177 |
} |
| 178 |
|
| 179 |
$verify_manually_action = $this->get_verification_action(); |
| 180 |
|
| 181 |
foreach ( $list as &$record ) { |
| 182 |
if ( empty( $record['actions']['value'] ) || |
| 183 |
! is_array( $record['actions']['value'] ) || |
| 184 |
empty( $sorted_tokens[ $record['choose']['value'] ] ) |
| 185 |
) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
$token_status = new Admin\Columns\Status_Column(); |
| 189 |
$column_status = $token_status->get_value( $sorted_tokens[ $record['choose']['value'] ] ); |
| 190 |
$column_status['text'] = $labels[ $column_status['status'] ] ?? $column_status['text']; |
| 191 |
|
| 192 |
$record['status']['value'] = $column_status; |
| 193 |
|
| 194 |
if ( Admin\Columns\Status_Column::PENDING !== $column_status['status'] ) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
$delete_action = array_pop( $record['actions']['value'] ); |
| 199 |
|
| 200 |
array_push( |
| 201 |
$record['actions']['value'], |
| 202 |
$verify_manually_action, |
| 203 |
$delete_action |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
return $list; |
| 208 |
} |
| 209 |
|
| 210 |
public function add_actions_for_records_page( array $config ): array { |
| 211 |
$delete_action = array_pop( $config['actions'] ); |
| 212 |
|
| 213 |
array_push( |
| 214 |
$config['actions'], |
| 215 |
$this->get_verification_action(), |
| 216 |
$delete_action |
| 217 |
); |
| 218 |
|
| 219 |
return $config; |
| 220 |
} |
| 221 |
|
| 222 |
protected function get_verification_action(): array { |
| 223 |
return array( |
| 224 |
'value' => 'verify', |
| 225 |
'label' => __( 'Verify manually', 'jet-form-builder' ), |
| 226 |
'endpoint' => ( new Rest_Endpoint( new Verify_Manually() ) )->to_array(), |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
public function remove_verify_button_for_records_single_page( array $config ): array { |
| 231 |
$box = false; |
| 232 |
foreach ( $config['containers'][1]['boxes'] as &$current_box ) { |
| 233 |
if ( 'verification' !== ( $current_box['slug'] ?? '' ) ) { |
| 234 |
continue; |
| 235 |
} |
| 236 |
$box =& $current_box; |
| 237 |
} |
| 238 |
|
| 239 |
if ( |
| 240 |
! $box || |
| 241 |
Status_Column::PENDING === $box['list']['status']['value']['status'] || |
| 242 |
empty( $box['box_actions'] ) |
| 243 |
) { |
| 244 |
return $config; |
| 245 |
} |
| 246 |
|
| 247 |
foreach ( $box['box_actions'] as $index => $action ) { |
| 248 |
if ( 'verify' !== $action['slug'] ) { |
| 249 |
continue; |
| 250 |
} |
| 251 |
unset( $box['box_actions'][ $index ] ); |
| 252 |
} |
| 253 |
|
| 254 |
$box['box_actions'] = array_values( $box['box_actions'] ); |
| 255 |
|
| 256 |
return $config; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* @param $record_id |
| 261 |
* @param $record_columns |
| 262 |
* |
| 263 |
* @throws Sql_Exception |
| 264 |
*/ |
| 265 |
public function connect_record_or_delete_token( $record_id, $record_columns ) { |
| 266 |
$token_id = jet_fb_context()->get_value( Verification::TOKEN_ID ); |
| 267 |
|
| 268 |
if ( ! $token_id ) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
$status = new Status_Info( $record_columns['status'] ); |
| 273 |
|
| 274 |
if ( ! $status->is_success() ) { |
| 275 |
( new Tokens_Model() )->delete( array( 'id' => $token_id ) ); |
| 276 |
|
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
( new Tokens_To_Records_Model() )->insert( |
| 281 |
array( |
| 282 |
'token_id' => $token_id, |
| 283 |
'record_id' => $record_id, |
| 284 |
) |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
private function get_verification_labels(): array { |
| 289 |
return array( |
| 290 |
Admin\Columns\Status_Column::VERIFIED => __( 'Verified', 'jet-form-builder' ), |
| 291 |
Admin\Columns\Status_Column::PENDING => __( 'Waiting verification', 'jet-form-builder' ), |
| 292 |
Admin\Columns\Status_Column::EXPIRED => __( 'Verification token expired', 'jet-form-builder' ), |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|