active-campaign-handler.php
4 years ago
forms-captcha.php
4 years ago
getresponse-handler.php
4 years ago
integration-base.php
4 years ago
mailchimp-handler.php
4 years ago
forms-captcha.php
162 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Integrations; |
| 4 | |
| 5 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 6 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 7 | use Jet_Form_Builder\Plugin; |
| 8 | |
| 9 | /** |
| 10 | * Captcha manager class |
| 11 | */ |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Define Forms_Captcha class |
| 20 | */ |
| 21 | class Forms_Captcha { |
| 22 | |
| 23 | const CAPTCHA_ACTION_PREFIX = 'jet_form_builder_captcha__'; |
| 24 | |
| 25 | public static $script_rendered = false; |
| 26 | |
| 27 | private $field_key = '_captcha_token'; |
| 28 | private $api = 'https://www.google.com/recaptcha/api/siteverify'; |
| 29 | private $defaults = array( |
| 30 | 'enabled' => false, |
| 31 | 'key' => '', |
| 32 | 'secret' => '', |
| 33 | ); |
| 34 | |
| 35 | public function __construct() { |
| 36 | add_filter( 'jet-form-builder/form-handler/form-data', array( $this, 'handle_request' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param $incoming_request |
| 41 | * |
| 42 | * @return mixed |
| 43 | * @throws Request_Exception |
| 44 | */ |
| 45 | public function handle_request( $incoming_request ) { |
| 46 | /** |
| 47 | * We get an array with the request body from the raw array, |
| 48 | * since only those fields that are in the form get into the processed request. |
| 49 | */ |
| 50 | $request = jet_fb_request_handler()->get_request(); |
| 51 | |
| 52 | if ( ! Plugin::instance()->captcha->verify( $request ) ) { |
| 53 | throw new Request_Exception( 'captcha_failed' ); |
| 54 | } |
| 55 | |
| 56 | return $incoming_request; |
| 57 | } |
| 58 | |
| 59 | private function api_front_url( $key ): string { |
| 60 | return esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) ); |
| 61 | } |
| 62 | |
| 63 | public function verify( $request ) { |
| 64 | $form_id = jet_fb_handler()->get_form_id(); |
| 65 | $captcha = $this->get_data( $form_id ); |
| 66 | |
| 67 | if ( empty( $captcha['enabled'] ) ) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | if ( empty( $request[ $this->field_key ] ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | $token = sanitize_text_field( $request[ $this->field_key ] ); |
| 76 | $response = wp_remote_post( |
| 77 | $this->api, |
| 78 | array( |
| 79 | 'body' => array( |
| 80 | 'secret' => $captcha['secret'], |
| 81 | 'response' => $token, |
| 82 | ), |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | $body = wp_remote_retrieve_body( $response ); |
| 87 | $body = json_decode( $body, true ); |
| 88 | |
| 89 | if ( ! empty( $body['action'] ) && ( self::CAPTCHA_ACTION_PREFIX . $form_id ) === $body['action'] ) { |
| 90 | return $body['success']; |
| 91 | } |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns captcha settings for passed form ID |
| 98 | * |
| 99 | * @param [type] $post_id [description] |
| 100 | * |
| 101 | * @return [type] [description] |
| 102 | */ |
| 103 | public function get_data( $form_id = null ) { |
| 104 | $captcha = Plugin::instance()->post_type->get_recaptcha( $form_id ); |
| 105 | |
| 106 | if ( ! $captcha || ! is_array( $captcha ) ) { |
| 107 | return $this->defaults; |
| 108 | } elseif ( isset( $captcha['use_global'] ) && $captcha['use_global'] ) { |
| 109 | return Tab_Handler_Manager::instance()->options( |
| 110 | 'captcha-tab', |
| 111 | array( 'enabled' => $captcha['enabled'] ) |
| 112 | ); |
| 113 | |
| 114 | } else { |
| 115 | return wp_parse_args( $captcha, $this->defaults ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function render( $form_id ) { |
| 120 | |
| 121 | $captcha = $this->get_data( $form_id ); |
| 122 | |
| 123 | if ( empty( $captcha['enabled'] ) || empty( $captcha['key'] ) ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | $key = esc_attr( $captcha['key'] ); |
| 128 | |
| 129 | wp_enqueue_script( |
| 130 | 'jet-form-builder-recaptcha', |
| 131 | $this->api_front_url( $key ), |
| 132 | array(), |
| 133 | jet_form_builder()->get_version(), |
| 134 | true |
| 135 | ); |
| 136 | |
| 137 | wp_enqueue_script( |
| 138 | 'jet-form-builder-recaptcha-handler', |
| 139 | jet_form_builder()->plugin_url( 'assets/js/re-captcha-v3.js' ), |
| 140 | array( 'jquery' ), |
| 141 | jet_form_builder()->get_version(), |
| 142 | true |
| 143 | ); |
| 144 | |
| 145 | $action_prefix = self::CAPTCHA_ACTION_PREFIX; |
| 146 | |
| 147 | wp_add_inline_script( |
| 148 | 'jet-form-builder-recaptcha-handler', |
| 149 | " |
| 150 | window.JetFormBuilderReCaptchaConfig = window.JetFormBuilderReCaptchaConfig || {}; |
| 151 | window.JetFormBuilderReCaptchaConfig[ $form_id ] = { key: '$key', action_prefix: '$action_prefix' }; |
| 152 | ", |
| 153 | 'before' |
| 154 | ); |
| 155 | |
| 156 | ?> |
| 157 | <input type="hidden" class="captcha-token" name="<?php echo esc_attr( $this->field_key ); ?>" value=""> |
| 158 | <?php |
| 159 | } |
| 160 | |
| 161 | } |
| 162 |