| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Captcha\Friendly_Captcha; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 7 |
use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha_From_Options; |
| 8 |
use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script; |
| 9 |
use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script; |
| 10 |
use JFB_Modules\Captcha\Module; |
| 11 |
use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 12 |
|
| 13 |
class Friendly_Captcha extends Base_Captcha_From_Options implements |
| 14 |
Captcha_Separate_Frontend_Script, |
| 15 |
Captcha_Separate_Editor_Script { |
| 16 |
|
| 17 |
public function get_id(): string { |
| 18 |
return 'friendly'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_title(): string { |
| 22 |
return __( 'Friendly Captcha', 'jet-form-builder' ); |
| 23 |
} |
| 24 |
|
| 25 |
public function verify( array $request ) { |
| 26 |
$action = ( new Verify_Token_Action() ) |
| 27 |
->set_secret( $this->options['secret'] ?? '' ) |
| 28 |
->set_site_key( $this->options['key'] ?? '' ) |
| 29 |
->set_solution( $request[ self::FIELD ] ?? '' ); |
| 30 |
|
| 31 |
try { |
| 32 |
$action->send_request(); |
| 33 |
} catch ( Gateway_Exception $exception ) { |
| 34 |
throw new Spam_Exception( |
| 35 |
Module::SPAM_EXCEPTION, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 36 |
$exception->getMessage(), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 37 |
...$exception->get_additional() // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public function render(): string { |
| 46 |
$captcha_args = apply_filters( |
| 47 |
'jet-form-builder/friendly-captcha/options', |
| 48 |
array( |
| 49 |
'sitekey' => $this->options['key'] ?? '', |
| 50 |
) |
| 51 |
); |
| 52 |
|
| 53 |
if ( empty( $captcha_args['sitekey'] ) ) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
$handle = $this->get_handle(); |
| 58 |
wp_enqueue_script( $handle ); |
| 59 |
|
| 60 |
/** |
| 61 |
* In some themes, the "the_content" filter may be executed before the "wp_enqueue_scripts" action. |
| 62 |
* Therefore, we should make sure that our script is registered before adding an inline script. |
| 63 |
*/ |
| 64 |
$this->register_frontend_scripts(); |
| 65 |
$this->module()->add_inline_config( $captcha_args, $handle ); |
| 66 |
|
| 67 |
return sprintf( |
| 68 |
'<div class="jet-form-builder-row captcha-token-container" data-validation-type="inherit"> |
| 69 |
<input type="hidden" class="%1$s" name="%2$s" value="" data-jfb-sync required="required"> |
| 70 |
<div class="captcha-token-container--inner"></div> |
| 71 |
</div>', |
| 72 |
self::FIELD_CLASS, |
| 73 |
self::FIELD |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function on_save_options( array $post_request ): array { |
| 78 |
// phpcs:disable WordPress.Security.NonceVerification.Missing |
| 79 |
$secret = sanitize_text_field( $post_request['secret'] ?? '' ); |
| 80 |
$key = sanitize_text_field( $post_request['key'] ?? '' ); |
| 81 |
|
| 82 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 83 |
|
| 84 |
return array( |
| 85 |
'secret' => $secret, |
| 86 |
'key' => $key, |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
public function enqueue_editor_script() { |
| 91 |
$script_asset = require_once $this->module()->get_dir( 'assets/build/friendly.captcha/editor.asset.php' ); |
| 92 |
|
| 93 |
if ( true === $script_asset ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
wp_enqueue_script( |
| 98 |
$this->get_handle(), |
| 99 |
$this->module()->get_url( 'assets/build/friendly.captcha/editor.js' ), |
| 100 |
$script_asset['dependencies'], |
| 101 |
$script_asset['version'], |
| 102 |
true |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
public function register_frontend_scripts() { |
| 107 |
$script_asset = require_once $this->module()->get_dir( 'assets/build/friendly.captcha/frontend.asset.php' ); |
| 108 |
|
| 109 |
// scripts have already registered |
| 110 |
if ( true === $script_asset ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
array_push( |
| 115 |
$script_asset['dependencies'], |
| 116 |
'jet-plugins' |
| 117 |
); |
| 118 |
|
| 119 |
wp_register_script( |
| 120 |
$this->get_handle(), |
| 121 |
$this->module()->get_url( 'assets/build/friendly.captcha/frontend.js' ), |
| 122 |
$script_asset['dependencies'], |
| 123 |
$script_asset['version'], |
| 124 |
true |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|