active-campaign
3 years ago
active-campaign-handler.php
3 years ago
forms-captcha.php
3 years ago
getresponse-handler.php
3 years ago
integration-base.php
3 years ago
mailchimp-handler.php
3 years ago
forms-captcha.php
156 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/request-handler/request', array( $this, 'handle_request' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param $request |
| 41 | * |
| 42 | * @return mixed |
| 43 | * @throws Request_Exception |
| 44 | */ |
| 45 | public function handle_request( $request ) { |
| 46 | if ( ! $this->verify( $request ) ) { |
| 47 | throw new Request_Exception( 'captcha_failed' ); |
| 48 | } |
| 49 | |
| 50 | return $request; |
| 51 | } |
| 52 | |
| 53 | private function api_front_url( $key ): string { |
| 54 | return esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) ); |
| 55 | } |
| 56 | |
| 57 | public function verify( $request ) { |
| 58 | $form_id = jet_fb_handler()->get_form_id(); |
| 59 | $captcha = $this->get_data( $form_id ); |
| 60 | |
| 61 | if ( empty( $captcha['enabled'] ) ) { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | if ( empty( $request[ $this->field_key ] ) ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $token = sanitize_text_field( $request[ $this->field_key ] ); |
| 70 | $response = wp_remote_post( |
| 71 | $this->api, |
| 72 | array( |
| 73 | 'body' => array( |
| 74 | 'secret' => $captcha['secret'], |
| 75 | 'response' => $token, |
| 76 | ), |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | $body = wp_remote_retrieve_body( $response ); |
| 81 | $body = json_decode( $body, true ); |
| 82 | |
| 83 | if ( ! empty( $body['action'] ) && ( self::CAPTCHA_ACTION_PREFIX . $form_id ) === $body['action'] ) { |
| 84 | return $body['success']; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns captcha settings for passed form ID |
| 92 | * |
| 93 | * @param [type] $post_id [description] |
| 94 | * |
| 95 | * @return [type] [description] |
| 96 | */ |
| 97 | public function get_data( $form_id = null ) { |
| 98 | $captcha = Plugin::instance()->post_type->get_recaptcha( $form_id ); |
| 99 | |
| 100 | if ( ! $captcha || ! is_array( $captcha ) ) { |
| 101 | return $this->defaults; |
| 102 | } elseif ( isset( $captcha['use_global'] ) && $captcha['use_global'] ) { |
| 103 | return Tab_Handler_Manager::instance()->options( |
| 104 | 'captcha-tab', |
| 105 | array( 'enabled' => $captcha['enabled'] ) |
| 106 | ); |
| 107 | |
| 108 | } else { |
| 109 | return wp_parse_args( $captcha, $this->defaults ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public function render( $form_id ) { |
| 114 | |
| 115 | $captcha = $this->get_data( $form_id ); |
| 116 | |
| 117 | if ( empty( $captcha['enabled'] ) || empty( $captcha['key'] ) ) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $key = esc_attr( $captcha['key'] ); |
| 122 | |
| 123 | wp_enqueue_script( |
| 124 | 'jet-form-builder-recaptcha', |
| 125 | $this->api_front_url( $key ), |
| 126 | array(), |
| 127 | jet_form_builder()->get_version(), |
| 128 | true |
| 129 | ); |
| 130 | |
| 131 | wp_enqueue_script( |
| 132 | 'jet-form-builder-recaptcha-handler', |
| 133 | jet_form_builder()->plugin_url( 'assets/js/re-captcha-v3.js' ), |
| 134 | array( 'jquery' ), |
| 135 | jet_form_builder()->get_version(), |
| 136 | true |
| 137 | ); |
| 138 | |
| 139 | $action_prefix = self::CAPTCHA_ACTION_PREFIX; |
| 140 | |
| 141 | wp_add_inline_script( |
| 142 | 'jet-form-builder-recaptcha-handler', |
| 143 | " |
| 144 | window.JetFormBuilderReCaptchaConfig = window.JetFormBuilderReCaptchaConfig || {}; |
| 145 | window.JetFormBuilderReCaptchaConfig[ $form_id ] = { key: '$key', action_prefix: '$action_prefix' }; |
| 146 | ", |
| 147 | 'before' |
| 148 | ); |
| 149 | |
| 150 | ?> |
| 151 | <input type="hidden" class="captcha-token" name="<?php echo esc_attr( $this->field_key ); ?>" value=""> |
| 152 | <?php |
| 153 | } |
| 154 | |
| 155 | } |
| 156 |