re-captcha-v3.php
196 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 | ->set_threshold( self::sanitize_threshold( $this->options['threshold'] ?? null ) ); |
| 45 | |
| 46 | try { |
| 47 | $action->send_request(); |
| 48 | } catch ( Gateway_Exception $exception ) { |
| 49 | throw new Spam_Exception( |
| 50 | Module::SPAM_EXCEPTION, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 51 | $exception->getMessage(), // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 52 | ...$exception->get_additional() // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return string |
| 59 | */ |
| 60 | public function render(): string { |
| 61 | $key = esc_attr( $this->options['key'] ?? '' ); |
| 62 | $url = esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) ); |
| 63 | |
| 64 | if ( ! $key || ! $url ) { |
| 65 | return ''; |
| 66 | } |
| 67 | |
| 68 | $handle = $this->get_handle(); |
| 69 | |
| 70 | wp_enqueue_script( |
| 71 | $handle . '-api', |
| 72 | $url, |
| 73 | array(), |
| 74 | '1.0.0', |
| 75 | true |
| 76 | ); |
| 77 | |
| 78 | $handle = $this->get_handle(); |
| 79 | wp_enqueue_script( $handle ); |
| 80 | |
| 81 | /** |
| 82 | * In some themes, the "the_content" filter may be executed before the "wp_enqueue_scripts" action. |
| 83 | * Therefore, we should make sure that our script is registered before adding an inline script. |
| 84 | */ |
| 85 | $this->register_frontend_scripts(); |
| 86 | $this->module()->add_inline_config( array( 'key' => $key ), $handle ); |
| 87 | |
| 88 | return sprintf( |
| 89 | '<input type="hidden" class="%1$s" name="%2$s" value=""/>', |
| 90 | self::FIELD_CLASS, |
| 91 | self::FIELD |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | public function sanitize_options( array $options ): Base_Captcha { |
| 96 | if ( isset( $options[ $this->get_id() ] ) ) { |
| 97 | parent::sanitize_options( $options ); |
| 98 | |
| 99 | return $this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * For backward compatibility |
| 104 | */ |
| 105 | $this->options = wp_array_slice_assoc( $options, array_keys( self::OPTIONS ) ); |
| 106 | |
| 107 | if ( empty( $options['use_global'] ) ) { |
| 108 | return $this; |
| 109 | } |
| 110 | |
| 111 | $this->options = array_merge( |
| 112 | $this->options, |
| 113 | $this->on_load_options() |
| 114 | ); |
| 115 | |
| 116 | return $this; |
| 117 | } |
| 118 | |
| 119 | public function on_save_options( array $post_request ): array { |
| 120 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 121 | $secret = sanitize_text_field( $post_request['secret'] ?? '' ); |
| 122 | $key = sanitize_text_field( $post_request['key'] ?? '' ); |
| 123 | $threshold = self::sanitize_threshold( $post_request['threshold'] ?? null ); |
| 124 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 125 | |
| 126 | return array( |
| 127 | 'secret' => $secret, |
| 128 | 'key' => $key, |
| 129 | 'threshold' => $threshold, |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @return array |
| 135 | * @see Captcha_Settings_From_Options |
| 136 | */ |
| 137 | public function on_load_options(): array { |
| 138 | $parent = parent::on_load_options(); |
| 139 | |
| 140 | if ( ! empty( $parent ) ) { |
| 141 | return $parent; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * For backward compatibility |
| 146 | */ |
| 147 | $options = Tab_Handler_Manager::get_options( 'captcha-tab', self::OPTIONS ); |
| 148 | |
| 149 | return wp_array_slice_assoc( $options, array_keys( self::OPTIONS ) ); |
| 150 | } |
| 151 | |
| 152 | private static function sanitize_threshold( $threshold ): float { |
| 153 | $threshold = filter_var( $threshold, FILTER_VALIDATE_FLOAT ); |
| 154 | |
| 155 | if ( false === $threshold || 0 > $threshold || 1 < $threshold ) { |
| 156 | return self::OPTIONS['threshold']; |
| 157 | } |
| 158 | |
| 159 | return (float) $threshold; |
| 160 | } |
| 161 | |
| 162 | public function enqueue_editor_script() { |
| 163 | $script_asset = require_once $this->module()->get_dir( 'assets/build/re-captcha-v3/editor.asset.php' ); |
| 164 | |
| 165 | wp_enqueue_script( |
| 166 | $this->get_handle(), |
| 167 | $this->module()->get_url( 'assets/build/re-captcha-v3/editor.js' ), |
| 168 | $script_asset['dependencies'], |
| 169 | $script_asset['version'], |
| 170 | true |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | public function register_frontend_scripts() { |
| 175 | $script_asset = require_once $this->module()->get_dir( 'assets/build/re-captcha-v3/frontend.asset.php' ); |
| 176 | |
| 177 | // scripts have already registered |
| 178 | if ( true === $script_asset ) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | array_push( |
| 183 | $script_asset['dependencies'], |
| 184 | 'jet-plugins' |
| 185 | ); |
| 186 | |
| 187 | wp_register_script( |
| 188 | $this->get_handle(), |
| 189 | $this->module()->get_url( 'assets/build/re-captcha-v3/frontend.js' ), |
| 190 | $script_asset['dependencies'], |
| 191 | $script_asset['version'], |
| 192 | true |
| 193 | ); |
| 194 | } |
| 195 | } |
| 196 |