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