re-captcha-v3.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Captcha\Re_Captcha_V3; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 7 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 8 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 9 | use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha; |
| 10 | use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha_From_Options; |
| 11 | use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script; |
| 12 | use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script; |
| 13 | use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Settings_From_Options; |
| 14 | use JFB_Modules\Captcha\Module; |
| 15 | use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 16 | |
| 17 | // If this file is called directly, abort. |
| 18 | if ( ! defined( 'WPINC' ) ) { |
| 19 | die; |
| 20 | } |
| 21 | |
| 22 | class Re_Captcha_V3 extends Base_Captcha_From_Options implements |
| 23 | Captcha_Separate_Frontend_Script, |
| 24 | Captcha_Separate_Editor_Script { |
| 25 | |
| 26 | const OPTIONS = array( |
| 27 | 'secret' => '', |
| 28 | 'key' => '', |
| 29 | 'threshold' => 0.5, |
| 30 | ); |
| 31 | |
| 32 | public function get_id(): string { |
| 33 | return 'google'; |
| 34 | } |
| 35 | |
| 36 | public function get_title(): string { |
| 37 | return __( 'reCAPTCHA v3', 'jet-form-builder' ); |
| 38 | } |
| 39 | |
| 40 | public function verify( array $request ) { |
| 41 | $action = ( new Verify_Token_Action() ) |
| 42 | ->set_secret( $this->options['secret'] ?? '' ) |
| 43 | ->set_token( $request[ self::FIELD ] ?? '' ); |
| 44 | |
| 45 | try { |
| 46 | $action->send_request(); |
| 47 | } catch ( Gateway_Exception $exception ) { |
| 48 | throw new Spam_Exception( |
| 49 | Module::SPAM_EXCEPTION, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 50 | $exception->getMessage(), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 51 | ...$exception->get_additional() // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return string |
| 58 | */ |
| 59 | public function render(): string { |
| 60 | $key = esc_attr( $this->options['key'] ?? '' ); |
| 61 | $url = esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) ); |
| 62 | |
| 63 | if ( ! $key || ! $url ) { |
| 64 | return ''; |
| 65 | } |
| 66 | |
| 67 | $handle = $this->get_handle(); |
| 68 | |
| 69 | wp_enqueue_script( |
| 70 | $handle . '-api', |
| 71 | $url, |
| 72 | array(), |
| 73 | '1.0.0', |
| 74 | true |
| 75 | ); |
| 76 | |
| 77 | $handle = $this->get_handle(); |
| 78 | wp_enqueue_script( $handle ); |
| 79 | |
| 80 | /** |
| 81 | * In some themes, the "the_content" filter may be executed before the "wp_enqueue_scripts" action. |
| 82 | * Therefore, we should make sure that our script is registered before adding an inline script. |
| 83 | */ |
| 84 | $this->register_frontend_scripts(); |
| 85 | $this->module()->add_inline_config( array( 'key' => $key ), $handle ); |
| 86 | |
| 87 | return sprintf( |
| 88 | '<input type="hidden" class="%1$s" name="%2$s" value=""/>', |
| 89 | self::FIELD_CLASS, |
| 90 | self::FIELD |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | public function sanitize_options( array $options ): Base_Captcha { |
| 95 | if ( isset( $options[ $this->get_id() ] ) ) { |
| 96 | parent::sanitize_options( $options ); |
| 97 | |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * For backward compatibility |
| 103 | */ |
| 104 | $this->options = wp_array_slice_assoc( $options, array_keys( self::OPTIONS ) ); |
| 105 | |
| 106 | if ( empty( $options['use_global'] ) ) { |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | $this->options = array_merge( |
| 111 | $this->options, |
| 112 | $this->on_load_options() |
| 113 | ); |
| 114 | |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | public function on_save_options( array $post_request ): array { |
| 119 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 120 | $secret = sanitize_text_field( $post_request['secret'] ?? '' ); |
| 121 | $key = sanitize_text_field( $post_request['key'] ?? '' ); |
| 122 | $threshold = filter_var( $post_request['threshold'] ?? '', FILTER_VALIDATE_FLOAT ); |
| 123 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 124 | |
| 125 | $threshold = false === $threshold ? self::OPTIONS['threshold'] : $threshold; |
| 126 | |
| 127 | return array( |
| 128 | 'secret' => $secret, |
| 129 | 'key' => $key, |
| 130 | 'threshold' => $threshold, |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return array |
| 136 | * @see Captcha_Settings_From_Options |
| 137 | */ |
| 138 | public function on_load_options(): array { |
| 139 | $parent = parent::on_load_options(); |
| 140 | |
| 141 | if ( ! empty( $parent ) ) { |
| 142 | return $parent; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * For backward compatibility |
| 147 | */ |
| 148 | $options = Tab_Handler_Manager::get_options( 'captcha-tab', self::OPTIONS ); |
| 149 | |
| 150 | return wp_array_slice_assoc( $options, array_keys( self::OPTIONS ) ); |
| 151 | } |
| 152 | |
| 153 | public function enqueue_editor_script() { |
| 154 | $script_asset = require_once $this->module()->get_dir( 'assets/build/re-captcha-v3/editor.asset.php' ); |
| 155 | |
| 156 | wp_enqueue_script( |
| 157 | $this->get_handle(), |
| 158 | $this->module()->get_url( 'assets/build/re-captcha-v3/editor.js' ), |
| 159 | $script_asset['dependencies'], |
| 160 | $script_asset['version'], |
| 161 | true |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | public function register_frontend_scripts() { |
| 166 | $script_asset = require_once $this->module()->get_dir( 'assets/build/re-captcha-v3/frontend.asset.php' ); |
| 167 | |
| 168 | // scripts have already registered |
| 169 | if ( true === $script_asset ) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | array_push( |
| 174 | $script_asset['dependencies'], |
| 175 | 'jet-plugins' |
| 176 | ); |
| 177 | |
| 178 | wp_register_script( |
| 179 | $this->get_handle(), |
| 180 | $this->module()->get_url( 'assets/build/re-captcha-v3/frontend.js' ), |
| 181 | $script_asset['dependencies'], |
| 182 | $script_asset['version'], |
| 183 | true |
| 184 | ); |
| 185 | } |
| 186 | } |
| 187 |