er is invalid or malformed.', 'elementor-pro' ),
'missing-input-response' => esc_html__( 'The response parameter is missing.', 'elementor-pro' ),
'invalid-input-response' => esc_html__( 'The response parameter is invalid or malformed.', 'elementor-pro' ),
];
$recaptcha_secret = static::get_secret_key();
$client_ip = Utils::get_client_ip();
$request = [
'body' => [
'secret' => $recaptcha_secret,
'response' => $recaptcha_response,
'remoteip' => $client_ip,
],
];
$response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $request );
$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== (int) $response_code ) {
/* translators: %d: Response code. */
$ajax_handler->add_error( $field['id'], sprintf( esc_html__( 'Can not connect to the reCAPTCHA server (%d).', 'elementor-pro' ), $response_code ) );
return;
}
$body = wp_remote_retrieve_body( $response );
$result = json_decode( $body, true );
if ( ! $this->validate_result( $result, $field ) ) {
$message = esc_html__( 'Invalid form, reCAPTCHA validation failed.', 'elementor-pro' );
if ( isset( $result['error-codes'] ) ) {
$result_errors = array_flip( $result['error-codes'] );
foreach ( $recaptcha_errors as $error_key => $error_desc ) {
if ( isset( $result_errors[ $error_key ] ) ) {
$message = $recaptcha_errors[ $error_key ];
break;
}
}
}
$this->add_error( $ajax_handler, $field, $message );
}
// If success - remove the field form list (don't send it in emails and etc )
$record->remove_field( $field['id'] );
}
/**
* @param Ajax_Handler $ajax_handler
* @param $field
* @param $message
*/
protected function add_error( $ajax_handler, $field, $message ) {
$ajax_handler->add_error( $field['id'], $message );
}
protected function validate_result( $result, $field ) {
if ( ! $result['success'] ) {
return false;
}
return true;
}
/**
* @param $item
* @param $item_index
* @param $widget Widget_Base
*/
public function render_field( $item, $item_index, $widget ) {
$recaptcha_html = '
';
// PHPCS - It's all escaped
echo $recaptcha_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @param $item
* @param $item_index
* @param $widget Widget_Base
*/
protected function add_render_attributes( $item, $item_index, $widget ) {
$recaptcha_name = static::get_recaptcha_name();
$widget->add_render_attribute( [
$recaptcha_name . $item_index => [
'class' => 'elementor-g-recaptcha',
'data-sitekey' => static::get_site_key(),
'data-type' => static::get_recaptcha_type(),
],
] );
$this->add_version_specific_render_attributes( $item, $item_index, $widget );
}
/**
* @param $item
* @param $item_index
* @param $widget Widget_Base
*/
protected function add_version_specific_render_attributes( $item, $item_index, $widget ) {
$recaptcha_name = static::get_recaptcha_name();
$widget->add_render_attribute( $recaptcha_name . $item_index, [
'data-theme' => $item['recaptcha_style'],
'data-size' => $item['recaptcha_size'],
] );
}
public function add_field_type( $field_types ) {
$field_types['recaptcha'] = esc_html__( 'reCAPTCHA', 'elementor-pro' );
return $field_types;
}
public function filter_field_item( $item ) {
if ( static::get_recaptcha_name() === $item['field_type'] ) {
$item['field_label'] = false;
}
return $item;
}
public function __construct() {
$this->register_scripts();
add_filter( 'elementor_pro/forms/field_types', [ $this, 'add_field_type' ] );
add_action( 'elementor_pro/forms/render_field/' . static::get_recaptcha_name(), [ $this, 'render_field' ], 10, 3 );
add_filter( 'elementor_pro/forms/render/item', [ $this, 'filter_field_item' ] );
add_filter( 'elementor_pro/editor/localize_settings', [ $this, 'localize_settings' ] );
if ( static::is_enabled() ) {
add_action( 'elementor_pro/forms/validation', [ $this, 'validation' ], 10, 2 );
add_action( 'elementor/preview/enqueue_scripts', [ $this, 'enqueue_scripts' ] );
}
if ( is_admin() ) {
add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_fields' ] );
}
}
}