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