This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhaul to execute at form submission (#2)
Core changes: * Removed older Caldera code they no longer have in mainline * Made it clearer when using v2 or v3 * Passing WordPress CodeSniffer rules * Script now injects at footer of page * Execution only happens at form submission * v3 implementation is less likely to make Google think we want a v2
- Loading branch information
Showing
3 changed files
with
103 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,112 @@ | ||
<?php echo $wrapper_before; ?> | ||
<?php echo empty( $field['config']['recapv'] ) ? $field_label : ''; ?> | ||
|
||
<?php | ||
/** | ||
* Renders the field in the visitor-facing form | ||
* | ||
* @package caldera-forms-anti-spam | ||
*/ | ||
|
||
$recaptcha_version = ( ! empty( $field['config']['recapv'] ) && 1 === $field['config']['recapv'] ) ? 'v3' : 'v2'; | ||
|
||
if(false !== strpos($field_input_class, 'has-error')){ | ||
// phpcs:disable | ||
echo $wrapper_before; | ||
// phpcs:enable | ||
|
||
if ( 'v2' === $recaptcha_version ) { | ||
echo esc_html( $field_label ); | ||
} | ||
|
||
if ( false !== strpos( $field_input_class, 'has-error' ) ) { | ||
echo '<span class="has-error">'; | ||
echo $field_caption; | ||
echo esc_html( $field_caption ); | ||
echo '</span>'; | ||
} | ||
|
||
if( empty( $field['config']['public_key'] ) ){ | ||
if ( empty( $field['config']['public_key'] ) ) { | ||
$field['config']['public_key'] = null; | ||
} | ||
|
||
/** | ||
We have moved the enqueue here as this allows us to alter the URL on the fly | ||
* We have moved the enqueue here as this allows us to alter the URL on the fly | ||
*/ | ||
$language = get_locale(); | ||
$script_url = "https://www.google.com/recaptcha/api.js?onload=cf_recaptcha_is_ready&render=explicit&hl=" . $language; | ||
if (!empty($field['config']['recapv']) && $field['config']['recapv'] === 1 ) { | ||
$script_url = "https://www.google.com/recaptcha/api.js?onload=cf_recaptcha_is_ready&render=" . trim($field['config']['public_key']) . "&hl=" . $language; | ||
if ( 'v3' === $recaptcha_version ) { | ||
$script_url = 'https://www.google.com/recaptcha/api.js?render=' . trim( $field['config']['public_key'] ) . '&hl=' . $language; | ||
} else { | ||
$script_url = 'https://www.google.com/recaptcha/api.js?render=explicit&onload=cf_recaptcha_is_ready&hl=' . $language; | ||
} | ||
wp_enqueue_script( 'cf-anti-spam-recapthca-lib', $script_url, array(), 1, true ); | ||
|
||
// phpcs:disable | ||
echo $field_before; | ||
// phpcs:enable | ||
|
||
if ( 'v3' === $recaptcha_version ) { | ||
echo '<div id="cap' . esc_html( $field_id ) . '"></div>'; | ||
} else { | ||
// phpcs:disable | ||
echo Caldera_Forms_Field_Input::html( $field, $field_structure, $form ); | ||
// phpcs:enable | ||
?> | ||
<div id="cap<?php echo esc_html( $field_id ); ?>" class="g-recaptcha" data-sitekey="<?php echo esc_html( $field['config']['public_key'] ); ?>" | ||
<?php | ||
if ( ! empty( $field['config']['invis'] ) && 1 === $field['config']['invis'] ) { | ||
echo ' data-size="invisible" ';} | ||
?> | ||
</div> | ||
<?php | ||
} | ||
wp_enqueue_script('cf-anti-spam-recapthca-lib', $script_url); | ||
|
||
?> | ||
|
||
<?php echo $field_before; ?> | ||
|
||
<?php echo Caldera_Forms_Field_Input::html( $field, $field_structure, $form ); ?> | ||
|
||
<?php if (!empty($field['config']['recapv']) && $field['config']['recapv'] === 1 ) { ?> | ||
<div id="cap<?php echo $field_id; ?>"></div> | ||
<?php } else { ?> | ||
<div id="cap<?php echo $field_id; ?>" class="g-recaptcha" data-sitekey="<?php echo $field['config']['public_key']; ?>" <?php if (!empty($field['config']['invis']) && $field['config']['invis'] === 1 ) { ?>data-size="invisible" <?php } ?>></div> | ||
<?php } ?> | ||
|
||
<?php echo $field_caption; ?> | ||
|
||
<?php echo $field_after; ?> | ||
// phpcs:disable | ||
echo $field_caption; | ||
echo $field_after; | ||
echo $wrapper_after; | ||
// phpcs:enable | ||
|
||
<?php | ||
echo $wrapper_after; | ||
ob_start(); | ||
ob_start(); | ||
?> | ||
|
||
<script> | ||
|
||
var cf_recaptcha_is_ready = function (){ | ||
jQuery(document).trigger("cf-anti-init-recaptcha"); | ||
} | ||
|
||
jQuery( function($){ | ||
|
||
jQuery(document).on("cf-anti-init-recaptcha", function(){ | ||
function init_recaptcha_<?php echo $field_id; ?>(){ | ||
|
||
var captch = $('#cap<?php echo $field_id; ?>'); | ||
<?php if (!empty($field['config']['recapv']) && $field['config']['recapv'] === 1 ) { ?> | ||
grecaptcha.ready(function() { | ||
var captch = $('#cap<?php echo $field_id; ?>'); | ||
grecaptcha.execute("<?php echo trim( $field['config']['public_key'] ); ?>", {action: 'homepage'}).then(function(token) { | ||
$('<input type="hidden" name="cf-recapv-token" value="' + token + '">').insertAfter(captch[0]); | ||
}); | ||
}); | ||
<?php } else { ?> | ||
|
||
captch.empty(); | ||
|
||
|
||
grecaptcha.render( captch[0], { "sitekey" : "<?php echo trim( $field['config']['public_key'] ); ?>", "theme" : "<?php echo isset( $field['config']['theme'] ) ? $field['config']['theme'] : "light"; ?>" } ); | ||
|
||
// Only load grecaptcha.execute if it's set to invisible mode. | ||
<?php if ( !empty( $field['config']['invis']) && $field['config']['invis'] === 1 ) { ?> grecaptcha.execute(); <?php } ?> | ||
<?php } ?> | ||
} | ||
|
||
jQuery(document).on('click', '.reset_<?php echo $field_id; ?>', function(e){ | ||
e.preventDefault(); | ||
init_recaptcha_<?php echo $field_id; ?>(); | ||
var cf_recaptcha_executed = false; | ||
var cf_recaptcha_execute = function ( event, obj ) { | ||
grecaptcha.ready( function () { | ||
var cf_form = jQuery('#cap<?php echo esc_html( $field_id ); ?>').parents('form:first'); | ||
var cf_form_name = cf_form.attr('aria-label').replace(/[^a-zA-Z]+/g, '_') || 'homepage'; | ||
grecaptcha.execute("<?php echo esc_html( trim( $field['config']['public_key'] ) ); ?>", {action: cf_form_name}).then( function (token) { | ||
var captch = jQuery('#cap<?php echo esc_html( $field_id ); ?>'); | ||
var captchToken = jQuery('#captoken<?php echo esc_html( $field_id ); ?>'); | ||
if (captchToken.length === 0) { | ||
jQuery('<input type="hidden" id="captoken<?php echo esc_html( $field_id ); ?>" name="cf-recapv-token" value="' + token + '">').insertAfter(captch[0]); | ||
} else { | ||
captchToken.val(token); | ||
} | ||
obj.inst.validationResult = true; | ||
cf_recaptcha_executed = true; | ||
cf_form.submit(); | ||
}); | ||
|
||
init_recaptcha_<?php echo $field_id; ?>(); | ||
}); | ||
} | ||
var cf_recaptcha_is_ready = function (){ | ||
<?php if ( 'v2' === $recaptcha_version ) { ?> | ||
var captch = jQuery('#cap<?php echo esc_html( $field_id ); ?>'); | ||
captch.empty(); | ||
|
||
grecaptcha.render( captch[0], { "sitekey" : "<?php echo esc_html( trim( $field['config']['public_key'] ) ); ?>", "theme" : "<?php echo esc_html( isset( $field['config']['theme'] ) ? $field['config']['theme'] : 'light' ); ?>" } ); | ||
|
||
// Only load grecaptcha.execute if it's set to invisible mode. | ||
<?php | ||
if ( ! empty( $field['config']['invis'] ) && 1 === $field['config']['invis'] ) { | ||
?> | ||
grecaptcha.execute(); <?php } ?> | ||
<?php } ?> | ||
} | ||
jQuery(document).on('cf.validate.FormSuccess', function( event, obj ){ | ||
if ( cf_recaptcha_executed === false ) { | ||
///make invalid until we finish the grecaptcha execution | ||
obj.inst.validationResult = false; | ||
cf_recaptcha_execute( event, obj ); | ||
} | ||
}); | ||
|
||
</script> | ||
<?php | ||
$script_template = ob_get_clean(); | ||
Caldera_Forms_Render_Util::add_inline_data( $script_template, $form ); | ||
|
||
<?php | ||
|
||
$script_template = ob_get_clean(); | ||
|
||
Caldera_Forms_Render_Util::add_inline_data( $script_template, $form ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters