hcaptcha.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Captcha\Hcaptcha; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Validation; |
| 7 | use Jet_Form_Builder\Classes\Builder_Helper; |
| 8 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 9 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 10 | use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha; |
| 11 | use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha_From_Options; |
| 12 | use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Frontend_Style_It; |
| 13 | use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script; |
| 14 | use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script; |
| 15 | use JFB_Modules\Captcha\Module; |
| 16 | use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 17 | |
| 18 | class Hcaptcha extends Base_Captcha_From_Options implements |
| 19 | Captcha_Separate_Frontend_Script, |
| 20 | Captcha_Separate_Editor_Script, |
| 21 | Captcha_Frontend_Style_It { |
| 22 | |
| 23 | public function get_id(): string { |
| 24 | return 'hcaptcha'; |
| 25 | } |
| 26 | |
| 27 | public function get_title(): string { |
| 28 | return __( 'hCaptcha', 'jet-form-builder' ); |
| 29 | } |
| 30 | |
| 31 | public function verify( array $request ) { |
| 32 | $action = ( new Verify_Token_Action() ) |
| 33 | ->set_secret( $this->options['secret'] ?? '' ) |
| 34 | ->set_token( $request[ self::FIELD ] ?? '' ) |
| 35 | ->set_action( jet_fb_live()->form_id ); |
| 36 | |
| 37 | try { |
| 38 | $action->send_request(); |
| 39 | } catch ( Gateway_Exception $exception ) { |
| 40 | throw new Spam_Exception( |
| 41 | 'captcha_failed', |
| 42 | $exception->getMessage(), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 43 | ...$exception->get_additional() // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return string |
| 50 | */ |
| 51 | public function render(): string { |
| 52 | $captcha_args = apply_filters( |
| 53 | 'jet-form-builder/h-captcha/options', |
| 54 | array( |
| 55 | 'sitekey' => $this->options['key'] ?? '', |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | if ( empty( $captcha_args['sitekey'] ) ) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | $handle = $this->get_handle( '-api' ); |
| 64 | wp_enqueue_script( $handle ); |
| 65 | |
| 66 | /** |
| 67 | * In some themes, the "the_content" filter may be executed before the "wp_enqueue_scripts" action. |
| 68 | * Therefore, we should make sure that our script is registered before adding an inline script. |
| 69 | */ |
| 70 | $this->register_frontend_scripts(); |
| 71 | $this->module()->add_inline_config( $captcha_args, $handle ); |
| 72 | |
| 73 | return sprintf( |
| 74 | '<div class="jet-form-builder-row captcha-token-container" data-validation-type="inherit"> |
| 75 | <input type="hidden" class="%1$s" name="%2$s" value="" data-jfb-sync required="required"> |
| 76 | </div>', |
| 77 | self::FIELD_CLASS, |
| 78 | self::FIELD |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | public function on_save_options( array $post_request ): array { |
| 83 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 84 | $secret = sanitize_text_field( $post_request['secret'] ?? '' ); |
| 85 | $key = sanitize_text_field( $post_request['key'] ?? '' ); |
| 86 | |
| 87 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 88 | |
| 89 | return array( |
| 90 | 'secret' => $secret, |
| 91 | 'key' => $key, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | public function enqueue_editor_script() { |
| 96 | wp_enqueue_script( |
| 97 | $this->module()->get_handle( $this->get_id() ), |
| 98 | $this->module()->get_url( 'assets-build/js/hcaptcha/editor.js' ), |
| 99 | array(), |
| 100 | jet_form_builder()->get_version(), |
| 101 | true |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | public function register_frontend_scripts() { |
| 106 | $handle = $this->get_handle(); |
| 107 | |
| 108 | if ( wp_script_is( $handle, 'registered' ) ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | $captcha_url = esc_url_raw( |
| 113 | apply_filters( |
| 114 | 'jet-form-builder/h-captcha/url', |
| 115 | 'https://js.hcaptcha.com/1/api.js?onload=jfbHCaptchaOnLoad&render=explicit' |
| 116 | ) |
| 117 | ); |
| 118 | |
| 119 | wp_register_script( |
| 120 | $handle, |
| 121 | $this->module()->get_url( 'assets-build/js/hcaptcha/frontend.js' ), |
| 122 | array( 'jet-plugins' ), |
| 123 | jet_form_builder()->get_version(), |
| 124 | true |
| 125 | ); |
| 126 | |
| 127 | wp_register_script( |
| 128 | $handle . '-api', |
| 129 | $captcha_url, |
| 130 | array( $handle ), |
| 131 | '1.0.0', |
| 132 | true |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | public function register_frontend_styles() { |
| 137 | wp_add_inline_style( |
| 138 | 'jet-form-builder-frontend', |
| 139 | ' |
| 140 | div[style*="z-index: 2147483647"] div[style*="border-width: 11px"][style*="position: absolute"][style*="pointer-events: none"] { |
| 141 | border-style: none; |
| 142 | } |
| 143 | ' |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 |