| 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 ] = esc_attr( $_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 |
} |