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