| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Captcha\Abstract_Captcha; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 7 |
use Jet_Form_Builder\Blocks\Exceptions\Render_Empty_Field; |
| 8 |
use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 9 |
use JFB_Components\Repository\Repository_Item_Instance_Trait; |
| 10 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 |
use Jet_Form_Builder\Exceptions\Request_Exception; |
| 12 |
use JFB_Components\Module\Base_Module_After_Install_It; |
| 13 |
use JFB_Components\Module\Base_Module_Dir_It; |
| 14 |
use JFB_Components\Module\Base_Module_Handle_It; |
| 15 |
use JFB_Components\Module\Base_Module_It; |
| 16 |
use JFB_Components\Module\Base_Module_Url_It; |
| 17 |
use JFB_Modules\Captcha\Module; |
| 18 |
use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 19 |
|
| 20 |
// If this file is called directly, abort. |
| 21 |
if ( ! defined( 'WPINC' ) ) { |
| 22 |
die; |
| 23 |
} |
| 24 |
|
| 25 |
abstract class Base_Captcha implements |
| 26 |
Repository_Item_Instance_Trait, |
| 27 |
Arrayable { |
| 28 |
|
| 29 |
protected $options; |
| 30 |
protected $locked_render = false; |
| 31 |
/** |
| 32 |
* @var bool |
| 33 |
*/ |
| 34 |
protected $exist_container; |
| 35 |
|
| 36 |
const FIELD_CLASS = 'captcha-token'; |
| 37 |
const FIELD = '_captcha_token'; |
| 38 |
const HANDLE_API = 'jet-form-builder-recaptcha'; |
| 39 |
const HANDLE_USER = self::HANDLE_API . '-handler'; |
| 40 |
|
| 41 |
abstract public function get_id(): string; |
| 42 |
|
| 43 |
abstract public function get_title(): string; |
| 44 |
|
| 45 |
/** |
| 46 |
* @param array $request |
| 47 |
* |
| 48 |
* @throws Spam_Exception |
| 49 |
*/ |
| 50 |
abstract public function verify( array $request ); |
| 51 |
|
| 52 |
abstract protected function render(): string; |
| 53 |
|
| 54 |
public function get_output(): string { |
| 55 |
return $this->render(); |
| 56 |
} |
| 57 |
|
| 58 |
public function sanitize_options( array $options ): Base_Captcha { |
| 59 |
$this->options = $options[ $this->get_id() ] ?? array(); |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function rep_item_id() { |
| 65 |
return $this->get_id(); |
| 66 |
} |
| 67 |
|
| 68 |
public function to_array(): array { |
| 69 |
return array( |
| 70 |
'label' => $this->get_title(), |
| 71 |
'value' => $this->get_id(), |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
public function lock_render() { |
| 76 |
$this->locked_render = true; |
| 77 |
} |
| 78 |
|
| 79 |
public function is_locked_render(): bool { |
| 80 |
return $this->locked_render; |
| 81 |
} |
| 82 |
|
| 83 |
public function get_handle( string $unique = '' ): string { |
| 84 |
return $this->module()->get_handle( $this->get_id() . $unique ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return Module|mixed |
| 89 |
*/ |
| 90 |
public function module() { |
| 91 |
try { |
| 92 |
return jet_form_builder()->module( 'captcha' ); |
| 93 |
} catch ( Repository_Exception $exception ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public function set_exist_container( bool $exist_container ) { |
| 99 |
$this->exist_container = $exist_container; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return bool|null |
| 104 |
*/ |
| 105 |
public function is_exist_container() { |
| 106 |
return $this->exist_container; |
| 107 |
} |
| 108 |
} |
| 109 |
|