PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.1.3
JetFormBuilder — Dynamic Blocks Form Builder v1.1.3
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / integrations / forms-captcha.php
jetformbuilder / includes / integrations Last commit date
active-campaign-handler.php 5 years ago forms-captcha.php 5 years ago getresponse-handler.php 5 years ago integration-base.php 5 years ago mailchimp-handler.php 5 years ago
forms-captcha.php
164 lines
1 <?php
2
3 namespace Jet_Form_Builder\Integrations;
4
5 use Jet_Form_Builder\Plugin;
6
7 /**
8 * Captcha manager class
9 */
10
11 // If this file is called directly, abort.
12 if ( ! defined( 'WPINC' ) ) {
13 die;
14 }
15
16 /**
17 * Define Forms_Captcha class
18 */
19 class Forms_Captcha {
20
21 public static $script_rendered = false;
22
23 private $field_key = '_captcha_token';
24 private $api = 'https://www.google.com/recaptcha/api/siteverify';
25 private $defaults = array(
26 'enabled' => false,
27 'key' => '',
28 'secret' => '',
29 );
30
31
32 public function verify( $form_id = null, $is_ajax = false ) {
33
34 $captcha = $this->get_data( $form_id );
35
36 if ( empty( $captcha['enabled'] ) ) {
37 return true;
38 }
39
40 $request = $this->sanitize_token_from_request( $is_ajax );
41
42 if ( empty( $request[ $this->field_key ] ) ) {
43 return false;
44 }
45
46 $token = esc_attr( $request[ $this->field_key ] );
47 $response = wp_remote_post( $this->api, array(
48 'body' => array(
49 'secret' => $captcha['secret'],
50 'response' => $token,
51 ),
52 ) );
53
54 $body = wp_remote_retrieve_body( $response );
55 $body = json_decode( $body, true );
56
57 if ( ! $body || empty( $body['success'] ) ) {
58 return false;
59 } else {
60 return $body['success'];
61 }
62
63 }
64
65 private function sanitize_token_from_request( $is_ajax ) {
66 $response = array();
67
68 if ( ! $is_ajax && isset( $_POST[ $this->field_key ] ) ) {
69 $response[ $this->field_key ] = sanitize_text_field( $_POST[ $this->field_key ] );
70
71 } else {
72 foreach ( $_REQUEST['values'] as $field ) {
73 if ( $field['name'] === $this->field_key ) {
74
75 $response[ $field['name'] ] = esc_attr( $field['value'] );
76 }
77 }
78 }
79 return $response;
80 }
81
82 /**
83 * Returns captcha settings for passed form ID
84 *
85 * @param [type] $post_id [description]
86 *
87 * @return [type] [description]
88 */
89 public function get_data( $form_id = null ) {
90
91 if ( ! $form_id ) {
92 $form_id = get_the_ID();
93 }
94
95 $captcha = Plugin::instance()->post_type->get_recaptcha( $form_id );
96
97 if ( ! $captcha || ! is_array( $captcha ) ) {
98 return $this->defaults;
99 } else {
100 return wp_parse_args( $captcha, $this->defaults );
101 }
102
103 }
104
105 public function render( $form_id ) {
106
107 $captcha = $this->get_data( $form_id );
108
109 if ( empty( $captcha['enabled'] ) ) {
110 return;
111 }
112
113 $key = esc_attr( $captcha['key'] );
114
115 if ( ! $key ) {
116 return;
117 }
118
119 if ( ! self::$script_rendered ) {
120 self::$script_rendered = true;
121 printf( '<script src="https://www.google.com/recaptcha/api.js?render=%s"></script>', $key );
122 }
123
124 ?>
125 <input type="hidden" class="captcha-token" name="<?php echo $this->field_key; ?>" value="">
126 <script>
127
128 if ( ! window.JetEngineFormCaptcha ) {
129 window.JetEngineFormCaptcha = function ( formID ) {
130
131 var $cpField = jQuery( 'form[data-form-id="' + formID + '"]' ).find( '.captcha-token' );
132
133 if ( window.JetEngineFormToken ) {
134 $cpField.val( window.JetEngineFormToken );
135 } else if ( window.grecaptcha ) {
136 window.grecaptcha.ready( function () {
137 grecaptcha.execute(
138 '<?php echo $key; ?>',
139 {
140 action: 'submit_form'
141 }
142 ).then( function ( token ) {
143 $cpField.val( token );
144 window.JetEngineFormToken = token;
145 } );
146 } );
147 }
148
149 }
150 }
151
152 window.JetEngineFormCaptcha( <?php echo $form_id; ?> );
153
154 jQuery( window ).on( 'jet-popup/show-event/after-show', function () {
155
156 window.JetEngineFormCaptcha( <?php echo $form_id; ?> );
157
158 } );
159 </script>
160 <?php
161
162 }
163
164 }