Contact Form 7 – reCaptcha v2 - Version 1.3.3

Version Description

(2020-03-01) = * Updated reCaptcha HTML to be wrapped in span tag as Contact Form 7 expects. * Updated javascript to reflect HTML change above.

Download this release

Release Info

Developer IQComputing
Plugin Icon 128x128 Contact Form 7 – reCaptcha v2
Version 1.3.3
Comparing to
See all releases

Code changes from version 1.3.2 to 1.3.3

assets/js/wpcf7-recaptcha-controls.js CHANGED
@@ -5,35 +5,35 @@ recaptchaCallback = function() {
5
  let pattern = /(^|\s)g-recaptcha(\s|$)/;
6
 
7
  for ( let i = 0; i < forms.length; i++ ) {
8
- let divs = forms[ i ].getElementsByTagName( 'div' );
9
 
10
- for ( let j = 0; j < divs.length; j++ ) {
11
- let sitekey = divs[ j ].getAttribute( 'data-sitekey' );
12
 
13
- if ( divs[ j ].className && divs[ j ].className.match( pattern ) && sitekey ) {
14
  let params = {
15
  'sitekey': sitekey,
16
- 'type': divs[ j ].getAttribute( 'data-type' ),
17
- 'size': divs[ j ].getAttribute( 'data-size' ),
18
- 'theme': divs[ j ].getAttribute( 'data-theme' ),
19
- 'align': divs[ j ].getAttribute( 'data-align' ),
20
- 'badge': divs[ j ].getAttribute( 'data-badge' ),
21
- 'tabindex': divs[ j ].getAttribute( 'data-tabindex' )
22
  };
23
 
24
- let callback = divs[ j ].getAttribute( 'data-callback' );
25
 
26
  if ( callback && 'function' == typeof window[ callback ] ) {
27
  params[ 'callback' ] = window[ callback ];
28
  }
29
 
30
- let expired_callback = divs[ j ].getAttribute( 'data-expired-callback' );
31
 
32
  if ( expired_callback && 'function' == typeof window[ expired_callback ] ) {
33
  params[ 'expired-callback' ] = window[ expired_callback ];
34
  }
35
 
36
- let widget_id = grecaptcha.render( divs[ j ], params );
37
  recaptchaWidgets.push( widget_id );
38
  break;
39
  }
@@ -58,28 +58,4 @@ document.addEventListener( 'wpcf7submit', function( event ) {
58
  grecaptcha.reset( recaptchaWidgets[ i ] );
59
  }
60
  }
61
- }, false );
62
-
63
- /**
64
- * Verify reCaptcha response and display error if not found.
65
- */
66
- document.addEventListener( 'wpcf7spam', function( event ) {
67
-
68
- let wpcf7forms = document.getElementsByClassName( 'wpcf7' );
69
-
70
- Array.prototype.forEach.call( wpcf7forms, function( form ) {
71
-
72
- if( form.getAttribute( 'id' ) != event.target.getAttribute( 'id' ) ) {
73
- return;
74
- }
75
-
76
- let response = form.querySelector( 'textarea[name="g-recaptcha-response"]' );
77
- let recaptcha = form.querySelector( 'div.wpcf7-recaptcha' );
78
- if( response && '' === response.value ) {
79
- let recaptchaWrapper = recaptcha.parentElement;
80
- wpcf7.notValidTip( recaptchaWrapper, wpcf7iqfix.recaptcha_empty );
81
- } else if( null === response ) {
82
- console.log( wpcf7iqfix.response_err );
83
- }
84
- } );
85
- } );
5
  let pattern = /(^|\s)g-recaptcha(\s|$)/;
6
 
7
  for ( let i = 0; i < forms.length; i++ ) {
8
+ let recaptchas = forms[ i ].getElementsByClassName( 'wpcf7-recaptcha' );
9
 
10
+ for ( let j = 0; j < recaptchas.length; j++ ) {
11
+ let sitekey = recaptchas[ j ].getAttribute( 'data-sitekey' );
12
 
13
+ if ( recaptchas[ j ].className && recaptchas[ j ].className.match( pattern ) && sitekey ) {
14
  let params = {
15
  'sitekey': sitekey,
16
+ 'type': recaptchas[ j ].getAttribute( 'data-type' ),
17
+ 'size': recaptchas[ j ].getAttribute( 'data-size' ),
18
+ 'theme': recaptchas[ j ].getAttribute( 'data-theme' ),
19
+ 'align': recaptchas[ j ].getAttribute( 'data-align' ),
20
+ 'badge': recaptchas[ j ].getAttribute( 'data-badge' ),
21
+ 'tabindex': recaptchas[ j ].getAttribute( 'data-tabindex' )
22
  };
23
 
24
+ let callback = recaptchas[ j ].getAttribute( 'data-callback' );
25
 
26
  if ( callback && 'function' == typeof window[ callback ] ) {
27
  params[ 'callback' ] = window[ callback ];
28
  }
29
 
30
+ let expired_callback = recaptchas[ j ].getAttribute( 'data-expired-callback' );
31
 
32
  if ( expired_callback && 'function' == typeof window[ expired_callback ] ) {
33
  params[ 'expired-callback' ] = window[ expired_callback ];
34
  }
35
 
36
+ let widget_id = grecaptcha.render( recaptchas[ j ], params );
37
  recaptchaWidgets.push( widget_id );
38
  break;
39
  }
58
  grecaptcha.reset( recaptchaWidgets[ i ] );
59
  }
60
  }
61
+ }, false );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
changelog.txt CHANGED
@@ -2,6 +2,18 @@
2
 
3
  This is to keep track of all changes the plugin undertakes. The readme.txt should only contain the most recent 3.
4
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  = 1.3.2 =
6
 
7
  Release Date: February 25, 2021
2
 
3
  This is to keep track of all changes the plugin undertakes. The readme.txt should only contain the most recent 3.
4
 
5
+ = 1.3.3 =
6
+
7
+ Release Date: March 01, 2021
8
+
9
+ * Overview
10
+ * Contact Form 7 expects span tags for Javascript Validation. The reCaptcha control was updated to be wrapped in HTML span tags.
11
+
12
+ * Code Changes
13
+ * Wrapped reCaptcha Control in HTML span tags.
14
+ * Added validation filter hooks `wpcf7_validate_recaptcha` and `wpcf7_validate_recaptcha*`
15
+ * Updated wpcf7-recaptcha-controls to account for span HTML tag changes.
16
+
17
  = 1.3.2 =
18
 
19
  Release Date: February 25, 2021
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: iqcomputing
3
  Tags: contact-form-7, contact-form-7-recaptcha, recaptcha, spam
4
  Requires at least: 4.9
5
  Tested up to: 5.6
6
- Stable tag: 1.3.2
7
  License: GPLv2 or later
8
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -60,12 +60,13 @@ Should the above be correct, at this point it's time to open a support thread fo
60
 
61
  == Changelog ==
62
 
 
 
 
 
63
  = 1.3.2 (2020-02-25) =
64
  * Updated the remove action hook priority for wpcf7_recaptcha_enqueue_scripts from 10 to 20
65
 
66
  = 1.3.0 (2020-12-21) =
67
  * Removed `iqfix_wpcf7_recaptcha_callback_script()` function
68
- * Enqueued reCaptcha controls based on whether google reCaptcha is enqueued.
69
-
70
- = 1.2.9 (2020-08-26) =
71
- * Updated external scripts enqueue method based on user feedback.
3
  Tags: contact-form-7, contact-form-7-recaptcha, recaptcha, spam
4
  Requires at least: 4.9
5
  Tested up to: 5.6
6
+ Stable tag: 1.3.3
7
  License: GPLv2 or later
8
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
9
 
60
 
61
  == Changelog ==
62
 
63
+ = 1.3.3 (2020-03-01) =
64
+ * Updated reCaptcha HTML to be wrapped in span tag as Contact Form 7 expects.
65
+ * Updated javascript to reflect HTML change above.
66
+
67
  = 1.3.2 (2020-02-25) =
68
  * Updated the remove action hook priority for wpcf7_recaptcha_enqueue_scripts from 10 to 20
69
 
70
  = 1.3.0 (2020-12-21) =
71
  * Removed `iqfix_wpcf7_recaptcha_callback_script()` function
72
+ * Enqueued reCaptcha controls based on whether google reCaptcha is enqueued.
 
 
 
recaptcha-v2.php CHANGED
@@ -119,10 +119,10 @@ function iqfix_wpcf7_recaptcha_form_tag_handler( $tag ) {
119
  wpcf7_form_controls_class( $tag->type, 'g-recaptcha' ) );
120
  $atts['id'] = $tag->get_id_option();
121
 
122
- $html = sprintf( '<div %1$s></div>', wpcf7_format_atts( $atts ) );
123
  $html .= iqfix_wpcf7_recaptcha_noscript(
124
  array( 'sitekey' => $atts['data-sitekey'] ) );
125
- $html = sprintf( '<div class="wpcf7-form-control-wrap">%s</div>', $html );
126
 
127
  return $html;
128
 
@@ -582,4 +582,38 @@ function iqfix_recaptcha_inline_css() {
582
  wp_add_inline_style( 'contact-form-7', $iqfix_css );
583
 
584
  }
585
- add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  wpcf7_form_controls_class( $tag->type, 'g-recaptcha' ) );
120
  $atts['id'] = $tag->get_id_option();
121
 
122
+ $html = sprintf( '<span %1$s></span>', wpcf7_format_atts( $atts ) );
123
  $html .= iqfix_wpcf7_recaptcha_noscript(
124
  array( 'sitekey' => $atts['data-sitekey'] ) );
125
+ $html = sprintf( '<span class="wpcf7-form-control-wrap recaptcha">%s</span>', $html );
126
 
127
  return $html;
128
 
582
  wp_add_inline_style( 'contact-form-7', $iqfix_css );
583
 
584
  }
585
+ add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' );
586
+
587
+
588
+ /**
589
+ * Validate empty reCaptcha
590
+ *
591
+ * @param Object $result
592
+ * @param Object $tag
593
+ *
594
+ * @return Object $result
595
+ */
596
+ function iqfix_recaptcha_validation( $result, $tag ) {
597
+
598
+ $tag->name = 'recaptcha';
599
+ if( ! isset( $_POST['g-recaptcha-response'] ) ) {
600
+
601
+ $result->invalidate(
602
+ $tag,
603
+ esc_html__( 'wpcf7-recaptcha: Could not verify reCaptcha response.', 'wpcf7-recaptcha' )
604
+ );
605
+
606
+ } else if( empty( $_POST['g-recaptcha-response'] ) ) {
607
+
608
+ $result->invalidate(
609
+ $tag,
610
+ esc_html__( 'Please verify that you are not a robot.', 'wpcf7-recaptcha' )
611
+ );
612
+
613
+ }
614
+
615
+ return $result;
616
+
617
+ }
618
+ add_filter( 'wpcf7_validate_recaptcha', 'iqfix_recaptcha_validation', 10, 2 );
619
+ add_filter( 'wpcf7_validate_recaptcha*', 'iqfix_recaptcha_validation', 10, 2 );
wpcf7-recaptcha.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * Plugin Name: ReCaptcha v2 for Contact Form 7
4
  * Description: ReCaptcha v2 Fix for Contact Form 7 5.1 and later.
5
- * Version: 1.3.2
6
  * Author: IQComputing
7
  * Author URI: http://www.iqcomputing.com/
8
  * License: GPL2
@@ -24,7 +24,7 @@ Class IQFix_WPCF7_Deity {
24
  *
25
  * @var String
26
  */
27
- public static $version = '1.3.2';
28
 
29
 
30
  /**
2
  /**
3
  * Plugin Name: ReCaptcha v2 for Contact Form 7
4
  * Description: ReCaptcha v2 Fix for Contact Form 7 5.1 and later.
5
+ * Version: 1.3.3
6
  * Author: IQComputing
7
  * Author URI: http://www.iqcomputing.com/
8
  * License: GPL2
24
  *
25
  * @var String
26
  */
27
+ public static $version = '1.3.3';
28
 
29
 
30
  /**