Contact Form 7 – reCaptcha v2 - Version 1.2.0

Version Description

(2019-06-13) = * Removed jQuery dependecy. * Converted jQuery reCaptcha submission check to vanilla JavaScript. * Updated reCaptcha noscript to account for A3 Lazy Load Plugin.

Download this release

Release Info

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

Code changes from version 1.1.9 to 1.2.0

Files changed (5) hide show
  1. changelog.txt +122 -109
  2. flamingo.php +78 -78
  3. readme.txt +74 -72
  4. recaptcha-v2.php +636 -637
  5. wpcf7-recaptcha.php +406 -406
changelog.txt CHANGED
@@ -1,110 +1,123 @@
1
- == Changelog Archive ==
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.1.9 =
6
-
7
- Release Date: May 14, 2019
8
-
9
- * Overview
10
- * Added option to allow user to choose which website to pull the reCaptcha resources from.
11
- The choices are google.com or recaptcha.net. These are for situations or countries that specifically block Google and prevent our reCaptcha from appearing.
12
-
13
- * Code Changes
14
- * Updated recaptcha-v2.php file to switch out the recaptcha source URLs.
15
- * Updated reCaptcha noscript to look nicer and function better.
16
- * We've left styling the noscript reCaptcha up to themes and developers.
17
- * Added private method to handle saving settings: IQFix_WPCF7_Deity::save_recaptcha_settings()
18
- * Added public static method to ensure my own sanity: IQFix_WPCF7_Deity::verify_recaptcha_source()
19
- * Makes sure that at every point our source is either google.com or recaptcha.net
20
-
21
- = 1.1.8 =
22
-
23
- Release Date: April 26, 2019
24
-
25
- * Overview
26
- * Contact Forms could still be submitted to Flamingo even when the reCaptcha was not being clicked or interacted with.
27
- We've hook into before Contact Form 7 does their Flamingo submission and removed the hook if 1) There's a reCaptcha on the form and 2) no reCaptcha verification was found in the submitted data.]
28
- This should prevent the multitude of spam posts being saved in Flamingo.
29
- Thanks to @scits to bringing this issue to our attention!
30
-
31
- * Files Added
32
- * wpcf7-recaptcha/flamingo.php
33
- * Only gets included and processed whenever Flamingo is detected to be installed and active.
34
-
35
- = 1.1.6 =
36
-
37
- Release Date: April 02, 2019
38
-
39
- * Overview
40
- * Forum user @aldebaran57 points out how to add localization to the reCaptcha API which was tested and integrated.
41
- * https://wordpress.org/support/topic/force-language-of-widget-so-it-reacts-to-language-switcher-on-multi-lang-sites/
42
-
43
- = 1.1.5 =
44
-
45
- Release Date: February 25, 2019
46
-
47
- * Overview
48
- * Added plugin to github and link to github on description page.
49
- * Ran into issue committing v1.1.4 which caused json checksums to not reflect some changes made. Thank @willpresleyev for pointing this out!
50
- * https://wordpress.org/support/topic/plugin-checksum-json-needs-updated/
51
-
52
- = 1.1.4 =
53
-
54
- Relase Date: February 22, 2019
55
-
56
- * General
57
- * Ensured WordPress 5.1 compatibility
58
- * We need your help translating this plugin! Please contribute at: https://translate.wordpress.org/projects/wp-plugins/wpcf7-recaptcha
59
-
60
- * Multisite
61
- * Multisite - Network Admins will notice a new menu item under plugins labelled "WPCF7 reCaptcha Settings"
62
- * Multisite - Network Admins now have the ability to add default keys and settings for sites. Individual sites can overwrite these defaults should they choose to.
63
- * Multisite - Default Network settings do not override keys or settings if they are already set/saved on the individual site. These only apply if none are found on the individual site.
64
-
65
- * Cleanup
66
- * Removed languages folder to avoid confusion with glotpress.
67
-
68
- = 1.1.3 =
69
-
70
- Relase Date: February 06, 2019
71
-
72
- * Overview
73
- * An attempt to make translations easier and better overall.
74
- * Combined a few redundant translation functions into a single translation function.
75
- * Changed instances of reCaptcha that were all caps and not consistent.
76
- * Added a margin-bottom: 0 style to the reCaptcha iframe in an attempt to prevent CSS overlapping using the `add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' )` hook.
77
-
78
-
79
- = 1.1.2 =
80
-
81
- Relase Date: January 14, 2019
82
-
83
- * Overview
84
- * Replaces old Text Domain 'iqc' with new Text Domain 'wpcf7-recaptcha' to match the plugin slug. This fix should help translations.
85
- * Added Portable Object Template (.pot) file to the /languages/ folder to help translators.
86
- * Added call to action on the settings page which encourages users to like IQComputing on Facebook to receive the latest news and updates regarding this plugin and future plugins we develop.
87
-
88
- = 1.1.1 =
89
-
90
- Release Date: January 07, 2019
91
-
92
- * Overview
93
- * Adds message whenever user forgets to click reCaptcha checkbox.
94
-
95
- = 1.1.0 =
96
-
97
- Release Date: December 28, 2018
98
-
99
- * Overview
100
- * Prevents Contact Form 7 from removing reCaptcha keys upon update.
101
- * Removes Contact Form 7 reCaptcha upgrade notice.
102
-
103
- = 1.0 =
104
-
105
- Release Date: December 28, 2018
106
-
107
- * Overview
108
- * Initial release of plugin.
109
- * Contact Form 7 5.0.5 reCaptcha functionality copied over.
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  * Created subpage under "Contact" named "reCaptcha Version" to be used as a settings page.
1
+ == Changelog Archive ==
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.2.0 =
6
+
7
+ Release Date: June 13, 2019
8
+
9
+ * Overview
10
+ * Removed jQuery dependecy entirely.
11
+ * Converted inline jQuery to JavaScript in regards to reCaptcha validation upon submission.
12
+ * Updated noscript reCaptcha to include a line break which A3 Lazy Load decides it won't lazy load for some reason.
13
+
14
+ * Code Changes
15
+ * recaptcha-v2.php -> iqfix_wpcf7_recaptcha_callback_script() Updated inline jQuery to vanilla JavaScript.
16
+ * recaptcha-v2.php -> iqfix_wpcf7_recaptcha_noscript() Added a linebreak to noscript iframe and textarea in an attempt to prevent A3 Lazy Load plugin from lazying loading the iframe.
17
+
18
+ = 1.1.9 =
19
+
20
+ Release Date: May 14, 2019
21
+
22
+ * Overview
23
+ * Added option to allow user to choose which website to pull the reCaptcha resources from.
24
+ The choices are google.com or recaptcha.net. These are for situations or countries that specifically block Google and prevent our reCaptcha from appearing.
25
+
26
+ * Code Changes
27
+ * Updated recaptcha-v2.php file to switch out the recaptcha source URLs.
28
+ * Updated reCaptcha noscript to look nicer and function better.
29
+ * We've left styling the noscript reCaptcha up to themes and developers.
30
+ * Added private method to handle saving settings: IQFix_WPCF7_Deity::save_recaptcha_settings()
31
+ * Added public static method to ensure my own sanity: IQFix_WPCF7_Deity::verify_recaptcha_source()
32
+ * Makes sure that at every point our source is either google.com or recaptcha.net
33
+
34
+ = 1.1.8 =
35
+
36
+ Release Date: April 26, 2019
37
+
38
+ * Overview
39
+ * Contact Forms could still be submitted to Flamingo even when the reCaptcha was not being clicked or interacted with.
40
+ We've hook into before Contact Form 7 does their Flamingo submission and removed the hook if 1) There's a reCaptcha on the form and 2) no reCaptcha verification was found in the submitted data.]
41
+ This should prevent the multitude of spam posts being saved in Flamingo.
42
+ Thanks to @scits to bringing this issue to our attention!
43
+
44
+ * Files Added
45
+ * wpcf7-recaptcha/flamingo.php
46
+ * Only gets included and processed whenever Flamingo is detected to be installed and active.
47
+
48
+ = 1.1.6 =
49
+
50
+ Release Date: April 02, 2019
51
+
52
+ * Overview
53
+ * Forum user @aldebaran57 points out how to add localization to the reCaptcha API which was tested and integrated.
54
+ * https://wordpress.org/support/topic/force-language-of-widget-so-it-reacts-to-language-switcher-on-multi-lang-sites/
55
+
56
+ = 1.1.5 =
57
+
58
+ Release Date: February 25, 2019
59
+
60
+ * Overview
61
+ * Added plugin to github and link to github on description page.
62
+ * Ran into issue committing v1.1.4 which caused json checksums to not reflect some changes made. Thank @willpresleyev for pointing this out!
63
+ * https://wordpress.org/support/topic/plugin-checksum-json-needs-updated/
64
+
65
+ = 1.1.4 =
66
+
67
+ Relase Date: February 22, 2019
68
+
69
+ * General
70
+ * Ensured WordPress 5.1 compatibility
71
+ * We need your help translating this plugin! Please contribute at: https://translate.wordpress.org/projects/wp-plugins/wpcf7-recaptcha
72
+
73
+ * Multisite
74
+ * Multisite - Network Admins will notice a new menu item under plugins labelled "WPCF7 reCaptcha Settings"
75
+ * Multisite - Network Admins now have the ability to add default keys and settings for sites. Individual sites can overwrite these defaults should they choose to.
76
+ * Multisite - Default Network settings do not override keys or settings if they are already set/saved on the individual site. These only apply if none are found on the individual site.
77
+
78
+ * Cleanup
79
+ * Removed languages folder to avoid confusion with glotpress.
80
+
81
+ = 1.1.3 =
82
+
83
+ Relase Date: February 06, 2019
84
+
85
+ * Overview
86
+ * An attempt to make translations easier and better overall.
87
+ * Combined a few redundant translation functions into a single translation function.
88
+ * Changed instances of reCaptcha that were all caps and not consistent.
89
+ * Added a margin-bottom: 0 style to the reCaptcha iframe in an attempt to prevent CSS overlapping using the `add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' )` hook.
90
+
91
+
92
+ = 1.1.2 =
93
+
94
+ Relase Date: January 14, 2019
95
+
96
+ * Overview
97
+ * Replaces old Text Domain 'iqc' with new Text Domain 'wpcf7-recaptcha' to match the plugin slug. This fix should help translations.
98
+ * Added Portable Object Template (.pot) file to the /languages/ folder to help translators.
99
+ * Added call to action on the settings page which encourages users to like IQComputing on Facebook to receive the latest news and updates regarding this plugin and future plugins we develop.
100
+
101
+ = 1.1.1 =
102
+
103
+ Release Date: January 07, 2019
104
+
105
+ * Overview
106
+ * Adds message whenever user forgets to click reCaptcha checkbox.
107
+
108
+ = 1.1.0 =
109
+
110
+ Release Date: December 28, 2018
111
+
112
+ * Overview
113
+ * Prevents Contact Form 7 from removing reCaptcha keys upon update.
114
+ * Removes Contact Form 7 reCaptcha upgrade notice.
115
+
116
+ = 1.0 =
117
+
118
+ Release Date: December 28, 2018
119
+
120
+ * Overview
121
+ * Initial release of plugin.
122
+ * Contact Form 7 5.0.5 reCaptcha functionality copied over.
123
  * Created subpage under "Contact" named "reCaptcha Version" to be used as a settings page.
flamingo.php CHANGED
@@ -1,79 +1,79 @@
1
- <?php
2
- /**
3
- * This file works with reCaptcha v2 to prevent Spam submissions from being stored.
4
- *
5
- * Originally, submitting a form without interacting with the reCaptcha box would submit to Contact Form 7
6
- * And end up being labelled as "Spam" by Flamingo.
7
- * This update will only check a submission _after_ reCaptcha has been compelted.
8
- */
9
-
10
-
11
- defined( 'ABSPATH' ) or die( "Hey, you're not a flamingo! Goodbye." );
12
-
13
-
14
- Class IQFix_Flamingo {
15
-
16
-
17
- /**
18
- * Class Registration, set up necessities
19
- *
20
- * @return void
21
- */
22
- public static function register() {
23
-
24
- $selection = WPCF7::get_option( 'iqfix_recaptcha' );
25
-
26
- if( empty( $selection ) ) {
27
- return;
28
- }
29
-
30
- $class = new self();
31
- $class->action_hooks();
32
-
33
- }
34
-
35
-
36
- /**
37
- * Add any necessary action hooks
38
- *
39
- * @return void
40
- */
41
- private function action_hooks() {
42
-
43
- // See: contact-form-7/modules/flamingo.php
44
- // Run before priority 10 to shortcircut Flamingo hook
45
- add_action( 'wpcf7_submit', array( $this, 'flamingo_acceptable_mail' ), 9, 2 );
46
-
47
- }
48
-
49
-
50
- /**
51
- * Remove the flamingo submission hook if our form has a ReCaptcha and an empty ReCaptcha response
52
- * @see contact-form-7/modules/flamingo.php LN7
53
- *
54
- * @param Array $types
55
- *
56
- * @return Array $types
57
- */
58
- public function flamingo_acceptable_mail( $wpcform, $result ) {
59
-
60
- $recaptcha = $wpcform->scan_form_tags( array( 'type' => 'recaptcha' ) );
61
-
62
- if( ! empty( $recaptcha ) ) {
63
-
64
- if( empty( $_POST['g-recaptcha-response'] ) ) {
65
-
66
- // Remove the Flamingo hook, don't run it until we have a successful recaptcha
67
- remove_action( 'wpcf7_submit', 'wpcf7_flamingo_submit', 10 );
68
-
69
- }
70
-
71
- }
72
-
73
- }
74
-
75
-
76
- } // END IQFix_Flamingo Class
77
-
78
-
79
  IQFix_Flamingo::register();
1
+ <?php
2
+ /**
3
+ * This file works with reCaptcha v2 to prevent Spam submissions from being stored.
4
+ *
5
+ * Originally, submitting a form without interacting with the reCaptcha box would submit to Contact Form 7
6
+ * And end up being labelled as "Spam" by Flamingo.
7
+ * This update will only check a submission _after_ reCaptcha has been compelted.
8
+ */
9
+
10
+
11
+ defined( 'ABSPATH' ) or die( "Hey, you're not a flamingo! Goodbye." );
12
+
13
+
14
+ Class IQFix_Flamingo {
15
+
16
+
17
+ /**
18
+ * Class Registration, set up necessities
19
+ *
20
+ * @return void
21
+ */
22
+ public static function register() {
23
+
24
+ $selection = WPCF7::get_option( 'iqfix_recaptcha' );
25
+
26
+ if( empty( $selection ) ) {
27
+ return;
28
+ }
29
+
30
+ $class = new self();
31
+ $class->action_hooks();
32
+
33
+ }
34
+
35
+
36
+ /**
37
+ * Add any necessary action hooks
38
+ *
39
+ * @return void
40
+ */
41
+ private function action_hooks() {
42
+
43
+ // See: contact-form-7/modules/flamingo.php
44
+ // Run before priority 10 to shortcircut Flamingo hook
45
+ add_action( 'wpcf7_submit', array( $this, 'flamingo_acceptable_mail' ), 9, 2 );
46
+
47
+ }
48
+
49
+
50
+ /**
51
+ * Remove the flamingo submission hook if our form has a ReCaptcha and an empty ReCaptcha response
52
+ * @see contact-form-7/modules/flamingo.php LN7
53
+ *
54
+ * @param Array $types
55
+ *
56
+ * @return Array $types
57
+ */
58
+ public function flamingo_acceptable_mail( $wpcform, $result ) {
59
+
60
+ $recaptcha = $wpcform->scan_form_tags( array( 'type' => 'recaptcha' ) );
61
+
62
+ if( ! empty( $recaptcha ) ) {
63
+
64
+ if( empty( $_POST['g-recaptcha-response'] ) ) {
65
+
66
+ // Remove the Flamingo hook, don't run it until we have a successful recaptcha
67
+ remove_action( 'wpcf7_submit', 'wpcf7_flamingo_submit', 10 );
68
+
69
+ }
70
+
71
+ }
72
+
73
+ }
74
+
75
+
76
+ } // END IQFix_Flamingo Class
77
+
78
+
79
  IQFix_Flamingo::register();
readme.txt CHANGED
@@ -1,72 +1,74 @@
1
- === Contact Form 7 - reCaptcha v2 ===
2
- Contributors: iqcomputing
3
- Tags: contact-form-7, contact-form-7-recaptcha, recaptcha, spam
4
- Requires at least: 4.9
5
- Tested up to: 5.2
6
- Stable tag: 1.1.9
7
- License: GPLv2 or later
8
- License URI: https://www.gnu.org/licenses/gpl-2.0.html
9
-
10
- Adds reCaptcha v2 from Contact Form 7 5.0.5 that was dropped on Contact Form 7 5.1
11
-
12
- == Description ==
13
-
14
- Contact Form 7 v5.1 dropped support for reCaptcha v2 along with the `[recaptcha]` tag December 2018. This plugin brings that functionality back from Contact Form 7 5.0.5 and re-adds the `[recaptcha]` tag.
15
-
16
- If this plugin is installed before updating Contact Form 7 from v5.0.5 to v5.1.1 then it will carry over your old API keys. At that point you will just need to head to this plugins settings page to tell the website to use reCaptcha v2.
17
-
18
- Once installed and configured it should be the same reCaptcha functionality you are used to in previous versions of Contact Form 7.
19
-
20
- == IQComputing ==
21
-
22
- * Like us on [Facebook](https://www.facebook.com/iqcomputing/ "IQComputing on Facebook")
23
- * Follow us on [Twitter](https://twitter.com/iqcomputing/ "IQComputing on Twitter")
24
- * Fork on [Github](https://github.com/IQComputing/wpcf7-recaptcha "IQComputing on Github")
25
-
26
- == Installation ==
27
-
28
- [Contact Form 7](https://wordpress.org/plugins/contact-form-7/ "Contact Form 7 plugin page") is required to work with this plugin.
29
-
30
- 1. Install this (Contact Form 7 - reCaptcha v2) plugin
31
- 1. Update Contact Form 7 to the latest version
32
- 1. Re-add the reCaptcha version 2 API keys (if no keys are currently set). For more information you may read [How to Generate Google reCAPTCHA v2 Keys](https://www.iqcomputing.com/support/articles/generate-google-recaptcha-v2-keys/) by IQComputing and [Contact Form 7 documentation](https://contactform7.com/recaptcha-v2/ "Contact Form 7 reCaptcha(v2)")
33
- 1. Using the left-hand admin navigation in the Contact Form 7 subpages click "reCaptcha Version" (Contact -> reCaptcha Version)
34
- 1. Once on the "Contact Form 7 - reCaptcha v2" settings page, select from the select list "reCaptcha Version 2" and click "save"
35
-
36
- Once the version 2 API keys are set, the version 2 has been selected in the plugin settings, all [recaptcha] tags will be replaced with the expected Google reCaptcha on all forms.
37
-
38
- == Frequently Asked Questions ==
39
-
40
- = Will this plugin work with Version 3 keys? =
41
-
42
- No and yes. Google reCaptcha has specific keys for each API. If you are doing an upgrade from a previous version of Contact Form 7 to the current version you will need to re-add the Version 2 API keys using the traditional method. That being said you can set the plugin usage to default and it will use the inherit Contact Form 7 reCaptcha Version 3 API.
43
-
44
- = Where do I add my Version 2 keys? =
45
-
46
- Under Contact -> Integration you can see a "reCAPTCHA" service box where you may either "Setup Integration" following instructions from the [Contact Form 7 documentation](https://contactform7.com/recaptcha-v2/ "Contact Form 7 reCaptcha(v2)"). If you already have keys set you will need to click the "Remove Keys" button and re-add them following the [Contact Form 7 documentation](https://contactform7.com/recaptcha-v2/ "Contact Form 7 reCaptcha(v2)").
47
-
48
- = I've added the Version 2 keys but nothing has changed =
49
-
50
- If you have added the Version 2 keys and you still do not see the reCaptcha show up on your forms please check the following:
51
-
52
- 1. In WordPress admin, under Contact -> reCaptcha Version please ensure that you have "reCaptcha Version 2" selected and saved.
53
- 1. On your contact forms please ensure that you have the [recaptcha] tag somewhere in the form.
54
-
55
- Should the above be correct, at this point it's time to open a support thread for us to look into the issue further.
56
-
57
- == Screenshots ==
58
-
59
- 1. Settings page
60
-
61
- == Changelog ==
62
-
63
- = 1.1.9 (2019-05-14) =
64
- * Added selectable option to choose which website to pull reCaptcha resources from (google.com | recaptcha.net) Thanks @scits!
65
- * Added and modified some class methods. See extended changelog for further details.
66
- * Modified noscript reCaptcha to look and function better "default". Styling left to theme authors and developers.
67
-
68
- = 1.1.8 (2019-04-26) =
69
- * Fixed WPCF7+Flamingo bug which saved un-verified submissions to spam in Flamingo. Thanks @scits!
70
-
71
- = 1.1.6 (2019-04-02) =
72
- * Added localization to reCaptcha API box which translates related text. Thanks @aldebaran57!
 
 
1
+ === Contact Form 7 - reCaptcha v2 ===
2
+ Contributors: iqcomputing
3
+ Tags: contact-form-7, contact-form-7-recaptcha, recaptcha, spam
4
+ Requires at least: 4.9
5
+ Tested up to: 5.2
6
+ Stable tag: 1.2.0
7
+ License: GPLv2 or later
8
+ License URI: https://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Adds reCaptcha v2 from Contact Form 7 5.0.5 that was dropped on Contact Form 7 5.1
11
+
12
+ == Description ==
13
+
14
+ Contact Form 7 v5.1 dropped support for reCaptcha v2 along with the `[recaptcha]` tag December 2018. This plugin brings that functionality back from Contact Form 7 5.0.5 and re-adds the `[recaptcha]` tag.
15
+
16
+ If this plugin is installed before updating Contact Form 7 from v5.0.5 to v5.1.1 then it will carry over your old API keys. At that point you will just need to head to this plugins settings page to tell the website to use reCaptcha v2.
17
+
18
+ Once installed and configured it should be the same reCaptcha functionality you are used to in previous versions of Contact Form 7.
19
+
20
+ == IQComputing ==
21
+
22
+ * Like us on [Facebook](https://www.facebook.com/iqcomputing/ "IQComputing on Facebook")
23
+ * Follow us on [Twitter](https://twitter.com/iqcomputing/ "IQComputing on Twitter")
24
+ * Fork on [Github](https://github.com/IQComputing/wpcf7-recaptcha "IQComputing on Github")
25
+
26
+ == Installation ==
27
+
28
+ [Contact Form 7](https://wordpress.org/plugins/contact-form-7/ "Contact Form 7 plugin page") is required to work with this plugin.
29
+
30
+ 1. Install this (Contact Form 7 - reCaptcha v2) plugin
31
+ 1. Update Contact Form 7 to the latest version
32
+ 1. Re-add the reCaptcha version 2 API keys (if no keys are currently set). For more information you may read [How to Generate Google reCAPTCHA v2 Keys](https://www.iqcomputing.com/support/articles/generate-google-recaptcha-v2-keys/) by IQComputing and [Contact Form 7 documentation](https://contactform7.com/recaptcha-v2/ "Contact Form 7 reCaptcha(v2)")
33
+ 1. Using the left-hand admin navigation in the Contact Form 7 subpages click "reCaptcha Version" (Contact -> reCaptcha Version)
34
+ 1. Once on the "Contact Form 7 - reCaptcha v2" settings page, select from the select list "reCaptcha Version 2" and click "save"
35
+
36
+ Once the version 2 API keys are set, the version 2 has been selected in the plugin settings, all [recaptcha] tags will be replaced with the expected Google reCaptcha on all forms.
37
+
38
+ == Frequently Asked Questions ==
39
+
40
+ = Will this plugin work with Version 3 keys? =
41
+
42
+ No and yes. Google reCaptcha has specific keys for each API. If you are doing an upgrade from a previous version of Contact Form 7 to the current version you will need to re-add the Version 2 API keys using the traditional method. That being said you can set the plugin usage to default and it will use the inherit Contact Form 7 reCaptcha Version 3 API.
43
+
44
+ = Where do I add my Version 2 keys? =
45
+
46
+ Under Contact -> Integration you can see a "reCAPTCHA" service box where you may either "Setup Integration" following instructions from the [Contact Form 7 documentation](https://contactform7.com/recaptcha-v2/ "Contact Form 7 reCaptcha(v2)"). If you already have keys set you will need to click the "Remove Keys" button and re-add them following the [Contact Form 7 documentation](https://contactform7.com/recaptcha-v2/ "Contact Form 7 reCaptcha(v2)").
47
+
48
+ = I've added the Version 2 keys but nothing has changed =
49
+
50
+ If you have added the Version 2 keys and you still do not see the reCaptcha show up on your forms please check the following:
51
+
52
+ 1. In WordPress admin, under Contact -> reCaptcha Version please ensure that you have "reCaptcha Version 2" selected and saved.
53
+ 1. On your contact forms please ensure that you have the [recaptcha] tag somewhere in the form.
54
+
55
+ Should the above be correct, at this point it's time to open a support thread for us to look into the issue further.
56
+
57
+ == Screenshots ==
58
+
59
+ 1. Settings page
60
+
61
+ == Changelog ==
62
+
63
+ = 1.2.0 (2019-06-13) =
64
+ * Removed jQuery dependecy.
65
+ * Converted jQuery reCaptcha submission check to vanilla JavaScript.
66
+ * Updated reCaptcha noscript to account for A3 Lazy Load Plugin.
67
+
68
+ = 1.1.9 (2019-05-14) =
69
+ * Added selectable option to choose which website to pull reCaptcha resources from (google.com | recaptcha.net) Thanks @scits!
70
+ * Added and modified some class methods. See extended changelog for further details.
71
+ * Modified noscript reCaptcha to look and function better "default". Styling left to theme authors and developers.
72
+
73
+ = 1.1.8 (2019-04-26) =
74
+ * Fixed WPCF7+Flamingo bug which saved un-verified submissions to spam in Flamingo. Thanks @scits!
recaptcha-v2.php CHANGED
@@ -1,638 +1,637 @@
1
- <?php
2
- /**
3
- * Replace reCaptcha v3 with ReCaptcha v2
4
- * Use [recaptcha] shortcode when possible
5
- */
6
-
7
-
8
- defined( 'ABSPATH' ) or die( 'You cannot be here.' );
9
-
10
-
11
- /**
12
- * Remove old hooks and add new hook callbacks
13
- *
14
- * @return void
15
- */
16
- function iqfix_wpcf7_manage_hooks() {
17
-
18
- // reCaptcha Verification
19
- remove_filter( 'wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9 );
20
- add_filter( 'wpcf7_spam', 'iqfix_wpcf7_recaptcha_check_with_google', 9 );
21
-
22
- // reCaptcha Enqueues
23
- remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 10 );
24
- add_action( 'wp_enqueue_scripts', 'iqfix_wpcf7_recaptcha_enqueue_scripts', 10 );
25
-
26
- // reCaptcha Footer Javascript
27
- remove_action( 'wp_footer', 'wpcf7_recaptcha_onload_script', 40 );
28
- add_action( 'wp_footer', 'iqfix_wpcf7_recaptcha_callback_script', 40 );
29
-
30
- }
31
- add_action( 'setup_theme', 'iqfix_wpcf7_manage_hooks' );
32
-
33
-
34
- /**
35
- * Remove current [recaptcha] tag and replace it with old reCaptcha tag
36
- *
37
- * @return void
38
- */
39
- function iqfix_wpcf7_add_recaptcha_tag() {
40
-
41
- wpcf7_remove_form_tag( 'recaptcha' );
42
- wpcf7_add_form_tag(
43
- 'recaptcha',
44
- 'iqfix_wpcf7_recaptcha_form_tag_handler',
45
- array( 'display-block' => true )
46
- );
47
-
48
- }
49
- add_action( 'wpcf7_init', 'iqfix_wpcf7_add_recaptcha_tag', 20 );
50
-
51
-
52
- /**
53
- * Register the Google reCaptcha API script
54
- *
55
- * The following function was not written by IQComputing and is included in
56
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
57
- * contact-form-7\modules\recaptcha.php LN241
58
- *
59
- * @return void
60
- */
61
- function iqfix_wpcf7_recaptcha_enqueue_scripts() {
62
-
63
- $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
64
- $source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
65
-
66
- $url = sprintf( 'https://www.%s/recaptcha/api.js', $source );
67
- $url = add_query_arg( array(
68
- 'hl' => esc_attr( get_locale() ), // Lowercase L
69
- 'onload' => 'recaptchaCallback',
70
- 'render' => 'explicit',
71
- ), $url );
72
-
73
- wp_register_script( 'google-recaptcha', $url, array( 'jquery' ), '2.0', true );
74
- wp_localize_script( 'google-recaptcha', 'wpcf7iqfix', array(
75
- 'recaptcha_empty' => esc_html__( 'Please verify that you are not a robot.', 'wpcf7-recaptcha' ),
76
- ) );
77
-
78
- }
79
- // See `iqfix_wpcf7_manage_hooks` callback above
80
-
81
-
82
- /**
83
- * reCaptcha Javascript
84
- *
85
- * The following function was not written by IQComputing and is included in
86
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
87
- * contact-form-7\modules\recaptcha.php LN253
88
- *
89
- * @return void
90
- */
91
- function iqfix_wpcf7_recaptcha_callback_script() {
92
-
93
- if ( ! wp_script_is( 'google-recaptcha', 'enqueued' ) ) {
94
- return;
95
- }
96
-
97
- ?>
98
- <script type="text/javascript">
99
- var recaptchaWidgets = [];
100
- var recaptchaCallback = function() {
101
- var forms = document.getElementsByTagName( 'form' );
102
- var pattern = /(^|\s)g-recaptcha(\s|$)/;
103
-
104
- for ( var i = 0; i < forms.length; i++ ) {
105
- var divs = forms[ i ].getElementsByTagName( 'div' );
106
-
107
- for ( var j = 0; j < divs.length; j++ ) {
108
- var sitekey = divs[ j ].getAttribute( 'data-sitekey' );
109
-
110
- if ( divs[ j ].className && divs[ j ].className.match( pattern ) && sitekey ) {
111
- var params = {
112
- 'sitekey': sitekey,
113
- 'type': divs[ j ].getAttribute( 'data-type' ),
114
- 'size': divs[ j ].getAttribute( 'data-size' ),
115
- 'theme': divs[ j ].getAttribute( 'data-theme' ),
116
- 'badge': divs[ j ].getAttribute( 'data-badge' ),
117
- 'tabindex': divs[ j ].getAttribute( 'data-tabindex' )
118
- };
119
-
120
- var callback = divs[ j ].getAttribute( 'data-callback' );
121
-
122
- if ( callback && 'function' == typeof window[ callback ] ) {
123
- params[ 'callback' ] = window[ callback ];
124
- }
125
-
126
- var expired_callback = divs[ j ].getAttribute( 'data-expired-callback' );
127
-
128
- if ( expired_callback && 'function' == typeof window[ expired_callback ] ) {
129
- params[ 'expired-callback' ] = window[ expired_callback ];
130
- }
131
-
132
- var widget_id = grecaptcha.render( divs[ j ], params );
133
- recaptchaWidgets.push( widget_id );
134
- break;
135
- }
136
- }
137
- }
138
- };
139
-
140
- document.addEventListener( 'wpcf7submit', function( event ) {
141
- switch ( event.detail.status ) {
142
- case 'spam':
143
- case 'mail_sent':
144
- case 'mail_failed':
145
- for ( var i = 0; i < recaptchaWidgets.length; i++ ) {
146
- grecaptcha.reset( recaptchaWidgets[ i ] );
147
- }
148
- }
149
- }, false );
150
-
151
- ( function( $ ) {
152
- $( '.wpcf7' ).on( 'wpcf7:spam', function( e ) {
153
- $( '[name="g-recaptcha-response"]', $( this ) ).each( function() {
154
- if ( '' === $( this ).val() ) {
155
- var $recaptcha = $( this ).closest( '.wpcf7-form-control-wrap' );
156
- wpcf7.notValidTip( $recaptcha, wpcf7iqfix.recaptcha_empty );
157
- }
158
- } );
159
- } );
160
- } )( jQuery );
161
- </script>
162
- <?php
163
-
164
- }
165
- // See `iqfix_wpcf7_manage_hooks` callback above
166
-
167
-
168
- /**
169
- * reCaptcha Callback
170
- *
171
- * The following function was not written by IQComputing and is included in
172
- * Contact Form 7 v5.0.5
173
- * contact-form-7\modules\recaptcha.php LN326
174
- *
175
- * @param WPCF7_FormTag $tag
176
- *
177
- * @return String $html
178
- */
179
- function iqfix_wpcf7_recaptcha_form_tag_handler( $tag ) {
180
-
181
- if ( ! wp_script_is( 'google-recaptcha', 'registered' ) ) {
182
- wpcf7_recaptcha_enqueue_scripts();
183
- }
184
-
185
- wp_enqueue_script( 'google-recaptcha' );
186
-
187
- $atts = array();
188
-
189
- $recaptcha = WPCF7_RECAPTCHA::get_instance();
190
- $atts['data-sitekey'] = $recaptcha->get_sitekey();
191
- $atts['data-type'] = $tag->get_option( 'type', '(audio|image)', true );
192
- $atts['data-size'] = $tag->get_option(
193
- 'size', '(compact|normal|invisible)', true );
194
- $atts['data-theme'] = $tag->get_option( 'theme', '(dark|light)', true );
195
- $atts['data-badge'] = $tag->get_option(
196
- 'badge', '(bottomright|bottomleft|inline)', true );
197
- $atts['data-tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
198
- $atts['data-callback'] = $tag->get_option( 'callback', '', true );
199
- $atts['data-expired-callback'] =
200
- $tag->get_option( 'expired_callback', '', true );
201
-
202
- $atts['class'] = $tag->get_class_option(
203
- wpcf7_form_controls_class( $tag->type, 'g-recaptcha' ) );
204
- $atts['id'] = $tag->get_id_option();
205
-
206
- $html = sprintf( '<div %1$s></div>', wpcf7_format_atts( $atts ) );
207
- $html .= iqfix_wpcf7_recaptcha_noscript(
208
- array( 'sitekey' => $atts['data-sitekey'] ) );
209
- $html = sprintf( '<div class="wpcf7-form-control-wrap">%s</div>', $html );
210
-
211
- return $html;
212
-
213
- }
214
- // See `iqfix_wpcf7_add_recaptcha_tag` callback above.
215
-
216
-
217
- /**
218
- * Display reCaptcha noscript tag should javacript be disabled.
219
- *
220
- * The following function was not written by IQComputing and is included in
221
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
222
- * contact-form-7\modules\recaptcha.php LN360
223
- *
224
- * @param Array $args
225
- *
226
- * @return String
227
- */
228
- function iqfix_wpcf7_recaptcha_noscript( $args = '' ) {
229
-
230
- $args = wp_parse_args( $args, array(
231
- 'sitekey' => '',
232
- ) );
233
-
234
- if ( empty( $args['sitekey'] ) ) {
235
- return;
236
- }
237
-
238
- $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
239
- $source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
240
- $url = add_query_arg( 'k', $args['sitekey'],
241
- sprintf( 'https://www.%s/recaptcha/api/fallback', $source )
242
- );
243
-
244
- ob_start();
245
- ?>
246
-
247
- <noscript>
248
- <div class="grecaptcha-noscript">
249
- <iframe src="<?php echo esc_url( $url ); ?>" frameborder="0" scrolling="no" width="310" height="430"></iframe>
250
- <textarea name="g-recaptcha-response" rows="3" cols="40" placeholder="<?php esc_attr_e( 'reCaptcha Response Here' ); ?>"></textarea>
251
- </div>
252
- </noscript>
253
- <?php
254
- return ob_get_clean();
255
- }
256
-
257
-
258
- /**
259
- * Verify submission is legitimate, verify reCaptcha response
260
- *
261
- * The following function was not written by IQComputing and is included in
262
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
263
- * contact-form-7\modules\recaptcha.php LN395
264
- *
265
- * @param Boolean $spam
266
- *
267
- * @return Boolean $spam
268
- */
269
- function iqfix_wpcf7_recaptcha_check_with_google( $spam ) {
270
-
271
- if ( $spam ) {
272
- return $spam;
273
- }
274
-
275
- $contact_form = wpcf7_get_current_contact_form();
276
-
277
- if ( ! $contact_form ) {
278
- return $spam;
279
- }
280
-
281
- $tags = $contact_form->scan_form_tags( array( 'type' => 'recaptcha' ) );
282
-
283
- if ( empty( $tags ) ) {
284
- return $spam;
285
- }
286
-
287
- $recaptcha = IQFix_ReCaptcha::get_instance();
288
-
289
- if( ! $recaptcha->is_active() ) {
290
- return $spam;
291
- }
292
-
293
- $response_token = wpcf7_recaptcha_response();
294
-
295
- $spam = ! $recaptcha->verify( $response_token );
296
-
297
- return $spam;
298
-
299
- }
300
- // See `iqfix_wpcf7_manage_hooks` callback above
301
-
302
-
303
- /**
304
- * Grab and return the posted reCaptcha response
305
- *
306
- * The following function was not written by IQComputing and is included in
307
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
308
- * contact-form-7\modules\recaptcha.php LN509
309
- *
310
- * @return String|FALSE
311
- */
312
- function wpcf7_recaptcha_response() {
313
-
314
- if ( isset( $_POST['g-recaptcha-response'] ) ) {
315
- return $_POST['g-recaptcha-response'];
316
- }
317
-
318
- return false;
319
-
320
- }
321
-
322
-
323
- /**
324
- * Add [recaptcha] to Contact Form 7 field list
325
- *
326
- * The following function was not written by IQComputing and is included in
327
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
328
- * contact-form-7\modules\recaptcha.php LN426
329
- *
330
- * @return void
331
- */
332
- function iqfix_wpcf7_add_tag_generator_recaptcha() {
333
-
334
- $tag_generator = WPCF7_TagGenerator::get_instance();
335
- $tag_generator->add(
336
- 'recaptcha',
337
- esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ),
338
- 'iqfix_wpcf7_tag_generator_recaptcha',
339
- array( 'nameless' => 1 )
340
- );
341
-
342
- }
343
- add_action( 'wpcf7_admin_init', 'iqfix_wpcf7_add_tag_generator_recaptcha', 45 );
344
-
345
-
346
- /**
347
- * Show [recaptcha] field settings
348
- *
349
- * The following function was not written by IQComputing and is included in
350
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
351
- * contact-form-7\modules\recaptcha.php LN432
352
- *
353
- * @param WPCF7_ContactForm $contact_form
354
- * @param Array $args
355
- *
356
- * @return void
357
- */
358
- function iqfix_wpcf7_tag_generator_recaptcha( $contact_form, $args = '' ) {
359
-
360
- $args = wp_parse_args( $args, array() );
361
- $recaptcha = IQFix_ReCaptcha::get_instance();
362
-
363
- if ( ! $recaptcha->is_active() ) {
364
-
365
- ?>
366
- <div class="control-box">
367
- <fieldset>
368
- <legend><?php
369
- /* translators: %s is a link to the Contact Form 7 blog post regarding reCaptcha v3 */
370
- echo sprintf( esc_html__( "To use reCaptcha, first you need to install an API key pair. For more details, see %s.", 'wpcf7-recaptcha' ), wpcf7_link( 'https://contactform7.com/recaptcha/', esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ) ) );
371
- ?></legend>
372
- </fieldset>
373
- </div>
374
- <?php
375
-
376
- return;
377
- }
378
-
379
- /* translators: %s is a link to the Contact Form 7 blog post regarding reCaptcha v3 */
380
- $description = esc_html__( "Generate a form-tag for a reCaptcha widget. For more details, see %s.", 'wpcf7-recaptcha' );
381
- $desc_link = wpcf7_link( 'https://contactform7.com/recaptcha/', esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ) );
382
-
383
- ?>
384
- <div class="control-box">
385
- <fieldset>
386
- <legend><?php echo sprintf( $description, $desc_link ); ?></legend>
387
-
388
- <table class="form-table">
389
- <tbody>
390
- <tr>
391
- <th scope="row"><?php
392
- /* translators: ReCaptcha size (normal or compact) */
393
- esc_html_e( 'Size', 'wpcf7-recaptcha' );
394
- ?></th>
395
- <td>
396
- <fieldset>
397
- <legend class="screen-reader-text"><?php
398
- /* translators: Screen reader text, reCaptcha size (normal or compact) */
399
- esc_html_e( 'Size', 'wpcf7-recaptcha' );
400
- ?></legend>
401
- <label for="<?php echo esc_attr( $args['content'] . '-size-normal' ); ?>"><input type="radio" name="size" class="option default" id="<?php echo esc_attr( $args['content'] . '-size-normal' ); ?>" value="normal" checked="checked" /> <?php /* translators: ReCaptcha size: normal */ esc_html_e( 'Normal', 'wpcf7-recaptcha' ); ?></label>
402
- <br />
403
- <label for="<?php echo esc_attr( $args['content'] . '-size-compact' ); ?>"><input type="radio" name="size" class="option" id="<?php echo esc_attr( $args['content'] . '-size-compact' ); ?>" value="compact" /> <?php /* translators: ReCaptcha size: compact */ esc_html_e( 'Compact', 'wpcf7-recaptcha' ); ?></label>
404
- </fieldset>
405
- </td>
406
- </tr>
407
-
408
- <tr>
409
- <th scope="row"><?php
410
- /* translators: ReCaptcha theme (light or dark) */
411
- esc_html_e( 'Theme', 'wpcf7-recaptcha' );
412
- ?></th>
413
- <td>
414
- <fieldset>
415
- <legend class="screen-reader-text"><?php
416
- /* translators: Screen reader text, reCaptcha theme (light or dark) */
417
- esc_html_e( 'Theme', 'wpcf7-recaptcha' );
418
- ?></legend>
419
- <label for="<?php echo esc_attr( $args['content'] . '-theme-light' ); ?>"><input type="radio" name="theme" class="option default" id="<?php echo esc_attr( $args['content'] . '-theme-light' ); ?>" value="light" checked="checked" /> <?php /* translators: ReCaptcha theme: light */ esc_html_e( 'Light', 'wpcf7-recaptcha' ); ?></label>
420
- <br />
421
- <label for="<?php echo esc_attr( $args['content'] . '-theme-dark' ); ?>"><input type="radio" name="theme" class="option" id="<?php echo esc_attr( $args['content'] . '-theme-dark' ); ?>" value="dark" /> <?php /* translators: ReCaptcha theme: dark */ esc_html_e( 'Dark', 'wpcf7-recaptcha' ); ?></label>
422
- </fieldset>
423
- </td>
424
- </tr>
425
-
426
- <tr>
427
- <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php
428
- /* translators: HTML Attribute ID for reCaptcha box */
429
- esc_html_e( 'Id attribute', 'wpcf7-recaptcha' );
430
- ?></label></th>
431
- <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
432
- </tr>
433
-
434
- <tr>
435
- <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php
436
- /* translators: HTML Attribute class for reCaptcha box */
437
- esc_html_e( 'Class attribute', 'wpcf7-recaptcha' );
438
- ?></label></th>
439
- <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
440
- </tr>
441
-
442
- </tbody>
443
- </table>
444
- </fieldset>
445
- </div>
446
-
447
- <div class="insert-box">
448
- <input type="text" name="recaptcha" class="tag code" readonly="readonly" onfocus="this.select()" />
449
-
450
- <div class="submitbox">
451
- <input type="button" class="button button-primary insert-tag" value="<?php
452
- /* translators: Insert shortcode tag into the page content */
453
- esc_attr_e( 'Insert Tag', 'wpcf7-recaptcha' );
454
- ?>" />
455
- </div>
456
- </div>
457
- <?php
458
-
459
- }
460
- // See `iqfix_wpcf7_add_tag_generator_recaptcha` callback above
461
-
462
-
463
- /**
464
- * The following class is supposed to use and replicate some functionality
465
- * From Contact Form 7 v5.0.5
466
- *
467
- * @return void
468
- */
469
- function iqfix_recaptcha_class_init() {
470
-
471
- if( ! class_exists( 'WPCF7_RECAPTCHA' ) ) {
472
- return false;
473
- }
474
-
475
- Class IQFix_ReCaptcha extends WPCF7_RECAPTCHA {
476
-
477
- private static $instance;
478
- private $sitekeys;
479
-
480
-
481
- /**
482
- * Class initialization
483
- *
484
- * The following method was not written by IQComputing and is included in
485
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
486
- * contact-form-7\modules\recaptcha.php LN202
487
- *
488
- * return void
489
- */
490
- private function __construct() {
491
- $this->sitekeys = WPCF7::get_option( 'recaptcha' );
492
- }
493
-
494
-
495
- /**
496
- * Singleton
497
- *
498
- * The following method was not written by IQComputing and is included in
499
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
500
- * contact-form-7\modules\recaptcha.php LN10
501
- *
502
- * @return IQFix_ReCaptcha
503
- */
504
- public static function get_instance() {
505
- if ( empty( self::$instance ) ) {
506
- self::$instance = new self;
507
- }
508
-
509
- return self::$instance;
510
- }
511
-
512
-
513
- /**
514
- * Check if reCaptcha is active
515
- *
516
- * The following method was not written by IQComputing and is included in
517
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
518
- * contact-form-7\modules\recaptcha.php LN26
519
- *
520
- * @return Boolean
521
- */
522
- public function is_active() {
523
- $sitekey = $this->get_sitekey();
524
- $secret = $this->get_secret( $sitekey );
525
- return $sitekey && $secret;
526
- }
527
-
528
-
529
- /**
530
- * Get set reCaptcha sitekey
531
- *
532
- * The following method was not written by IQComputing and is included in
533
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
534
- * contact-form-7\modules\recaptcha.php LN45
535
- *
536
- * @return String|FALSE
537
- */
538
- public function get_sitekey() {
539
- if ( empty( $this->sitekeys )
540
- or ! is_array( $this->sitekeys ) ) {
541
- return false;
542
- }
543
-
544
- $sitekeys = array_keys( $this->sitekeys );
545
-
546
- return $sitekeys[0];
547
- }
548
-
549
-
550
- /**
551
- * Get set reCaptcha secret key
552
- *
553
- * The following method was not written by IQComputing and is included in
554
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
555
- * contact-form-7\modules\recaptcha.php LN55
556
- *
557
- * @return String|FALSE
558
- */
559
- public function get_secret( $sitekey ) {
560
- $sitekeys = (array) $this->sitekeys;
561
-
562
- if ( isset( $sitekeys[$sitekey] ) ) {
563
- return $sitekeys[$sitekey];
564
- } else {
565
- return false;
566
- }
567
- }
568
-
569
-
570
- /**
571
- * Verify reCaptcha Response
572
- *
573
- * The following method was not written by IQComputing and is included in
574
- * Contact Form 7 v5.0.5 by Takayuki Miyoshi
575
- * contact-form-7\modules\recaptcha.php LN65
576
- *
577
- * @param String $response_token
578
- *
579
- * @return Boolean $is_human
580
- */
581
- public function verify( $response_token ) {
582
-
583
- $is_human = false;
584
-
585
- if ( empty( $response_token ) ) {
586
- return $is_human;
587
- }
588
-
589
- $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
590
- $source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
591
- $url = sprintf( 'https://www.%s/recaptcha/api/siteverify', $source );
592
- $sitekey = $this->get_sitekey();
593
- $secret = $this->get_secret( $sitekey );
594
-
595
- $response = wp_safe_remote_post( $url, array(
596
- 'body' => array(
597
- 'secret' => $secret,
598
- 'response' => $response_token,
599
- 'remoteip' => $_SERVER['REMOTE_ADDR'],
600
- ),
601
- ) );
602
-
603
- if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
604
-
605
- if ( WP_DEBUG ) {
606
- $this->log( $endpoint, $request, $response );
607
- }
608
-
609
- return $is_human;
610
-
611
- }
612
-
613
- $response = wp_remote_retrieve_body( $response );
614
- $response = json_decode( $response, true );
615
- $is_human = isset( $response['success'] ) && true == $response['success'];
616
-
617
- return apply_filters( 'wpcf7_recaptcha_verify_response', $is_human, $response );
618
-
619
- }
620
-
621
- }
622
-
623
- }
624
- add_action( 'init', 'iqfix_recaptcha_class_init', 11 );
625
-
626
-
627
- /**
628
- * Add some inline CSS for the reCaptcha iframe
629
- *
630
- * @return void
631
- */
632
- function iqfix_recaptcha_inline_css() {
633
-
634
- $iqfix_css ='.wpcf7 .wpcf7-recaptcha iframe {margin-bottom: 0;}';
635
- wp_add_inline_style( 'contact-form-7', $iqfix_css );
636
-
637
- }
638
  add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' );
1
+ <?php
2
+ /**
3
+ * Replace reCaptcha v3 with ReCaptcha v2
4
+ * Use [recaptcha] shortcode when possible
5
+ */
6
+
7
+
8
+ defined( 'ABSPATH' ) or die( 'You cannot be here.' );
9
+
10
+
11
+ /**
12
+ * Remove old hooks and add new hook callbacks
13
+ *
14
+ * @return void
15
+ */
16
+ function iqfix_wpcf7_manage_hooks() {
17
+
18
+ // reCaptcha Verification
19
+ remove_filter( 'wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9 );
20
+ add_filter( 'wpcf7_spam', 'iqfix_wpcf7_recaptcha_check_with_google', 9 );
21
+
22
+ // reCaptcha Enqueues
23
+ remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 10 );
24
+ add_action( 'wp_enqueue_scripts', 'iqfix_wpcf7_recaptcha_enqueue_scripts', 10 );
25
+
26
+ // reCaptcha Footer Javascript
27
+ remove_action( 'wp_footer', 'wpcf7_recaptcha_onload_script', 40 );
28
+ add_action( 'wp_footer', 'iqfix_wpcf7_recaptcha_callback_script', 40 );
29
+
30
+ }
31
+ add_action( 'setup_theme', 'iqfix_wpcf7_manage_hooks' );
32
+
33
+
34
+ /**
35
+ * Remove current [recaptcha] tag and replace it with old reCaptcha tag
36
+ *
37
+ * @return void
38
+ */
39
+ function iqfix_wpcf7_add_recaptcha_tag() {
40
+
41
+ wpcf7_remove_form_tag( 'recaptcha' );
42
+ wpcf7_add_form_tag(
43
+ 'recaptcha',
44
+ 'iqfix_wpcf7_recaptcha_form_tag_handler',
45
+ array( 'display-block' => true )
46
+ );
47
+
48
+ }
49
+ add_action( 'wpcf7_init', 'iqfix_wpcf7_add_recaptcha_tag', 20 );
50
+
51
+
52
+ /**
53
+ * Register the Google reCaptcha API script
54
+ *
55
+ * The following function was not written by IQComputing and is included in
56
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
57
+ * contact-form-7\modules\recaptcha.php LN241
58
+ *
59
+ * @return void
60
+ */
61
+ function iqfix_wpcf7_recaptcha_enqueue_scripts() {
62
+
63
+ $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
64
+ $source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
65
+
66
+ $url = sprintf( 'https://www.%s/recaptcha/api.js', $source );
67
+ $url = add_query_arg( array(
68
+ 'hl' => esc_attr( get_locale() ), // Lowercase L
69
+ 'onload' => 'recaptchaCallback',
70
+ 'render' => 'explicit',
71
+ ), $url );
72
+
73
+ wp_register_script( 'google-recaptcha', $url, array(), '2.0', true );
74
+ wp_localize_script( 'google-recaptcha', 'wpcf7iqfix', array(
75
+ 'recaptcha_empty' => esc_html__( 'Please verify that you are not a robot.', 'wpcf7-recaptcha' ),
76
+ ) );
77
+
78
+ }
79
+ // See `iqfix_wpcf7_manage_hooks` callback above
80
+
81
+
82
+ /**
83
+ * reCaptcha Javascript
84
+ *
85
+ * @return void
86
+ */
87
+ function iqfix_wpcf7_recaptcha_callback_script() {
88
+
89
+ if ( ! wp_script_is( 'google-recaptcha', 'enqueued' ) ) {
90
+ return;
91
+ }
92
+
93
+ ?>
94
+ <script type="text/javascript">
95
+ var recaptchaWidgets = [];
96
+ var recaptchaCallback = function() {
97
+ var forms = document.getElementsByTagName( 'form' );
98
+ var pattern = /(^|\s)g-recaptcha(\s|$)/;
99
+
100
+ for ( var i = 0; i < forms.length; i++ ) {
101
+ var divs = forms[ i ].getElementsByTagName( 'div' );
102
+
103
+ for ( var j = 0; j < divs.length; j++ ) {
104
+ var sitekey = divs[ j ].getAttribute( 'data-sitekey' );
105
+
106
+ if ( divs[ j ].className && divs[ j ].className.match( pattern ) && sitekey ) {
107
+ var params = {
108
+ 'sitekey': sitekey,
109
+ 'type': divs[ j ].getAttribute( 'data-type' ),
110
+ 'size': divs[ j ].getAttribute( 'data-size' ),
111
+ 'theme': divs[ j ].getAttribute( 'data-theme' ),
112
+ 'badge': divs[ j ].getAttribute( 'data-badge' ),
113
+ 'tabindex': divs[ j ].getAttribute( 'data-tabindex' )
114
+ };
115
+
116
+ var callback = divs[ j ].getAttribute( 'data-callback' );
117
+
118
+ if ( callback && 'function' == typeof window[ callback ] ) {
119
+ params[ 'callback' ] = window[ callback ];
120
+ }
121
+
122
+ var expired_callback = divs[ j ].getAttribute( 'data-expired-callback' );
123
+
124
+ if ( expired_callback && 'function' == typeof window[ expired_callback ] ) {
125
+ params[ 'expired-callback' ] = window[ expired_callback ];
126
+ }
127
+
128
+ var widget_id = grecaptcha.render( divs[ j ], params );
129
+ recaptchaWidgets.push( widget_id );
130
+ break;
131
+ }
132
+ }
133
+ }
134
+ };
135
+
136
+ document.addEventListener( 'wpcf7submit', function( event ) {
137
+ switch ( event.detail.status ) {
138
+ case 'spam':
139
+ case 'mail_sent':
140
+ case 'mail_failed':
141
+ for ( var i = 0; i < recaptchaWidgets.length; i++ ) {
142
+ grecaptcha.reset( recaptchaWidgets[ i ] );
143
+ }
144
+ }
145
+ }, false );
146
+
147
+ document.addEventListener( 'wpcf7spam', function( event ) {
148
+ var wpcf7forms = document.getElementsByClassName( 'wpcf7' );
149
+ Array.prototype.forEach.call( wpcf7forms, function( form ) {
150
+ var response = form.querySelector( 'input[name="g-recaptcha-response"]' );
151
+ var recaptcha = form.querySelector( 'div.wpcf7-recaptcha' );
152
+ if( '' === response.value ) {
153
+ var recaptchaWrapper = recaptcha.parentElement;
154
+ wpcf7.notValidTip( recaptchaWrapper, wpcf7iqfix.recaptcha_empty );
155
+ }
156
+ } );
157
+ } );
158
+ </script>
159
+ <?php
160
+
161
+ }
162
+ // See `iqfix_wpcf7_manage_hooks` callback above
163
+
164
+
165
+ /**
166
+ * reCaptcha Callback
167
+ *
168
+ * The following function was not written by IQComputing and is included in
169
+ * Contact Form 7 v5.0.5
170
+ * contact-form-7\modules\recaptcha.php LN326
171
+ *
172
+ * @param WPCF7_FormTag $tag
173
+ *
174
+ * @return String $html
175
+ */
176
+ function iqfix_wpcf7_recaptcha_form_tag_handler( $tag ) {
177
+
178
+ if ( ! wp_script_is( 'google-recaptcha', 'registered' ) ) {
179
+ wpcf7_recaptcha_enqueue_scripts();
180
+ }
181
+
182
+ wp_enqueue_script( 'google-recaptcha' );
183
+
184
+ $atts = array();
185
+
186
+ $recaptcha = WPCF7_RECAPTCHA::get_instance();
187
+ $atts['data-sitekey'] = $recaptcha->get_sitekey();
188
+ $atts['data-type'] = $tag->get_option( 'type', '(audio|image)', true );
189
+ $atts['data-size'] = $tag->get_option(
190
+ 'size', '(compact|normal|invisible)', true );
191
+ $atts['data-theme'] = $tag->get_option( 'theme', '(dark|light)', true );
192
+ $atts['data-badge'] = $tag->get_option(
193
+ 'badge', '(bottomright|bottomleft|inline)', true );
194
+ $atts['data-tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
195
+ $atts['data-callback'] = $tag->get_option( 'callback', '', true );
196
+ $atts['data-expired-callback'] =
197
+ $tag->get_option( 'expired_callback', '', true );
198
+
199
+ $atts['class'] = $tag->get_class_option(
200
+ wpcf7_form_controls_class( $tag->type, 'g-recaptcha' ) );
201
+ $atts['id'] = $tag->get_id_option();
202
+
203
+ $html = sprintf( '<div %1$s></div>', wpcf7_format_atts( $atts ) );
204
+ $html .= iqfix_wpcf7_recaptcha_noscript(
205
+ array( 'sitekey' => $atts['data-sitekey'] ) );
206
+ $html = sprintf( '<div class="wpcf7-form-control-wrap">%s</div>', $html );
207
+
208
+ return $html;
209
+
210
+ }
211
+ // See `iqfix_wpcf7_add_recaptcha_tag` callback above.
212
+
213
+
214
+ /**
215
+ * Display reCaptcha noscript tag should javacript be disabled.
216
+ *
217
+ * The following function was not written by IQComputing and is included in
218
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
219
+ * contact-form-7\modules\recaptcha.php LN360
220
+ *
221
+ * @param Array $args
222
+ *
223
+ * @return String
224
+ */
225
+ function iqfix_wpcf7_recaptcha_noscript( $args = '' ) {
226
+
227
+ $args = wp_parse_args( $args, array(
228
+ 'sitekey' => '',
229
+ ) );
230
+
231
+ if ( empty( $args['sitekey'] ) ) {
232
+ return;
233
+ }
234
+
235
+ $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
236
+ $source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
237
+ $url = add_query_arg( 'k', $args['sitekey'],
238
+ sprintf( 'https://www.%s/recaptcha/api/fallback', $source )
239
+ );
240
+
241
+ ob_start();
242
+ ?>
243
+
244
+ <noscript>
245
+ <div class="grecaptcha-noscript">
246
+ <iframe src="<?php echo esc_url( $url ); ?>" frameborder="0" scrolling="no" width="310" height="430">
247
+ </iframe>
248
+ <textarea name="g-recaptcha-response" rows="3" cols="40" placeholder="<?php esc_attr_e( 'reCaptcha Response Here' ); ?>">
249
+ </textarea>
250
+ </div>
251
+ </noscript>
252
+ <?php
253
+ return ob_get_clean();
254
+ }
255
+
256
+
257
+ /**
258
+ * Verify submission is legitimate, verify reCaptcha response
259
+ *
260
+ * The following function was not written by IQComputing and is included in
261
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
262
+ * contact-form-7\modules\recaptcha.php LN395
263
+ *
264
+ * @param Boolean $spam
265
+ *
266
+ * @return Boolean $spam
267
+ */
268
+ function iqfix_wpcf7_recaptcha_check_with_google( $spam ) {
269
+
270
+ if ( $spam ) {
271
+ return $spam;
272
+ }
273
+
274
+ $contact_form = wpcf7_get_current_contact_form();
275
+
276
+ if ( ! $contact_form ) {
277
+ return $spam;
278
+ }
279
+
280
+ $tags = $contact_form->scan_form_tags( array( 'type' => 'recaptcha' ) );
281
+
282
+ if ( empty( $tags ) ) {
283
+ return $spam;
284
+ }
285
+
286
+ $recaptcha = IQFix_ReCaptcha::get_instance();
287
+
288
+ if( ! $recaptcha->is_active() ) {
289
+ return $spam;
290
+ }
291
+
292
+ $response_token = wpcf7_recaptcha_response();
293
+
294
+ $spam = ! $recaptcha->verify( $response_token );
295
+
296
+ return $spam;
297
+
298
+ }
299
+ // See `iqfix_wpcf7_manage_hooks` callback above
300
+
301
+
302
+ /**
303
+ * Grab and return the posted reCaptcha response
304
+ *
305
+ * The following function was not written by IQComputing and is included in
306
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
307
+ * contact-form-7\modules\recaptcha.php LN509
308
+ *
309
+ * @return String|FALSE
310
+ */
311
+ function wpcf7_recaptcha_response() {
312
+
313
+ if ( isset( $_POST['g-recaptcha-response'] ) ) {
314
+ return $_POST['g-recaptcha-response'];
315
+ }
316
+
317
+ return false;
318
+
319
+ }
320
+
321
+
322
+ /**
323
+ * Add [recaptcha] to Contact Form 7 field list
324
+ *
325
+ * The following function was not written by IQComputing and is included in
326
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
327
+ * contact-form-7\modules\recaptcha.php LN426
328
+ *
329
+ * @return void
330
+ */
331
+ function iqfix_wpcf7_add_tag_generator_recaptcha() {
332
+
333
+ $tag_generator = WPCF7_TagGenerator::get_instance();
334
+ $tag_generator->add(
335
+ 'recaptcha',
336
+ esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ),
337
+ 'iqfix_wpcf7_tag_generator_recaptcha',
338
+ array( 'nameless' => 1 )
339
+ );
340
+
341
+ }
342
+ add_action( 'wpcf7_admin_init', 'iqfix_wpcf7_add_tag_generator_recaptcha', 45 );
343
+
344
+
345
+ /**
346
+ * Show [recaptcha] field settings
347
+ *
348
+ * The following function was not written by IQComputing and is included in
349
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
350
+ * contact-form-7\modules\recaptcha.php LN432
351
+ *
352
+ * @param WPCF7_ContactForm $contact_form
353
+ * @param Array $args
354
+ *
355
+ * @return void
356
+ */
357
+ function iqfix_wpcf7_tag_generator_recaptcha( $contact_form, $args = '' ) {
358
+
359
+ $args = wp_parse_args( $args, array() );
360
+ $recaptcha = IQFix_ReCaptcha::get_instance();
361
+
362
+ if ( ! $recaptcha->is_active() ) {
363
+
364
+ ?>
365
+ <div class="control-box">
366
+ <fieldset>
367
+ <legend><?php
368
+ /* translators: %s is a link to the Contact Form 7 blog post regarding reCaptcha v3 */
369
+ echo sprintf( esc_html__( "To use reCaptcha, first you need to install an API key pair. For more details, see %s.", 'wpcf7-recaptcha' ), wpcf7_link( 'https://contactform7.com/recaptcha/', esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ) ) );
370
+ ?></legend>
371
+ </fieldset>
372
+ </div>
373
+ <?php
374
+
375
+ return;
376
+ }
377
+
378
+ /* translators: %s is a link to the Contact Form 7 blog post regarding reCaptcha v3 */
379
+ $description = esc_html__( "Generate a form-tag for a reCaptcha widget. For more details, see %s.", 'wpcf7-recaptcha' );
380
+ $desc_link = wpcf7_link( 'https://contactform7.com/recaptcha/', esc_html__( 'reCaptcha', 'wpcf7-recaptcha' ) );
381
+
382
+ ?>
383
+ <div class="control-box">
384
+ <fieldset>
385
+ <legend><?php echo sprintf( $description, $desc_link ); ?></legend>
386
+
387
+ <table class="form-table">
388
+ <tbody>
389
+ <tr>
390
+ <th scope="row"><?php
391
+ /* translators: ReCaptcha size (normal or compact) */
392
+ esc_html_e( 'Size', 'wpcf7-recaptcha' );
393
+ ?></th>
394
+ <td>
395
+ <fieldset>
396
+ <legend class="screen-reader-text"><?php
397
+ /* translators: Screen reader text, reCaptcha size (normal or compact) */
398
+ esc_html_e( 'Size', 'wpcf7-recaptcha' );
399
+ ?></legend>
400
+ <label for="<?php echo esc_attr( $args['content'] . '-size-normal' ); ?>"><input type="radio" name="size" class="option default" id="<?php echo esc_attr( $args['content'] . '-size-normal' ); ?>" value="normal" checked="checked" /> <?php /* translators: ReCaptcha size: normal */ esc_html_e( 'Normal', 'wpcf7-recaptcha' ); ?></label>
401
+ <br />
402
+ <label for="<?php echo esc_attr( $args['content'] . '-size-compact' ); ?>"><input type="radio" name="size" class="option" id="<?php echo esc_attr( $args['content'] . '-size-compact' ); ?>" value="compact" /> <?php /* translators: ReCaptcha size: compact */ esc_html_e( 'Compact', 'wpcf7-recaptcha' ); ?></label>
403
+ </fieldset>
404
+ </td>
405
+ </tr>
406
+
407
+ <tr>
408
+ <th scope="row"><?php
409
+ /* translators: ReCaptcha theme (light or dark) */
410
+ esc_html_e( 'Theme', 'wpcf7-recaptcha' );
411
+ ?></th>
412
+ <td>
413
+ <fieldset>
414
+ <legend class="screen-reader-text"><?php
415
+ /* translators: Screen reader text, reCaptcha theme (light or dark) */
416
+ esc_html_e( 'Theme', 'wpcf7-recaptcha' );
417
+ ?></legend>
418
+ <label for="<?php echo esc_attr( $args['content'] . '-theme-light' ); ?>"><input type="radio" name="theme" class="option default" id="<?php echo esc_attr( $args['content'] . '-theme-light' ); ?>" value="light" checked="checked" /> <?php /* translators: ReCaptcha theme: light */ esc_html_e( 'Light', 'wpcf7-recaptcha' ); ?></label>
419
+ <br />
420
+ <label for="<?php echo esc_attr( $args['content'] . '-theme-dark' ); ?>"><input type="radio" name="theme" class="option" id="<?php echo esc_attr( $args['content'] . '-theme-dark' ); ?>" value="dark" /> <?php /* translators: ReCaptcha theme: dark */ esc_html_e( 'Dark', 'wpcf7-recaptcha' ); ?></label>
421
+ </fieldset>
422
+ </td>
423
+ </tr>
424
+
425
+ <tr>
426
+ <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php
427
+ /* translators: HTML Attribute ID for reCaptcha box */
428
+ esc_html_e( 'Id attribute', 'wpcf7-recaptcha' );
429
+ ?></label></th>
430
+ <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td>
431
+ </tr>
432
+
433
+ <tr>
434
+ <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php
435
+ /* translators: HTML Attribute class for reCaptcha box */
436
+ esc_html_e( 'Class attribute', 'wpcf7-recaptcha' );
437
+ ?></label></th>
438
+ <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td>
439
+ </tr>
440
+
441
+ </tbody>
442
+ </table>
443
+ </fieldset>
444
+ </div>
445
+
446
+ <div class="insert-box">
447
+ <input type="text" name="recaptcha" class="tag code" readonly="readonly" onfocus="this.select()" />
448
+
449
+ <div class="submitbox">
450
+ <input type="button" class="button button-primary insert-tag" value="<?php
451
+ /* translators: Insert shortcode tag into the page content */
452
+ esc_attr_e( 'Insert Tag', 'wpcf7-recaptcha' );
453
+ ?>" />
454
+ </div>
455
+ </div>
456
+ <?php
457
+
458
+ }
459
+ // See `iqfix_wpcf7_add_tag_generator_recaptcha` callback above
460
+
461
+
462
+ /**
463
+ * The following class is supposed to use and replicate some functionality
464
+ * From Contact Form 7 v5.0.5
465
+ *
466
+ * @return void
467
+ */
468
+ function iqfix_recaptcha_class_init() {
469
+
470
+ if( ! class_exists( 'WPCF7_RECAPTCHA' ) ) {
471
+ return false;
472
+ }
473
+
474
+ Class IQFix_ReCaptcha extends WPCF7_RECAPTCHA {
475
+
476
+ private static $instance;
477
+ private $sitekeys;
478
+
479
+
480
+ /**
481
+ * Class initialization
482
+ *
483
+ * The following method was not written by IQComputing and is included in
484
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
485
+ * contact-form-7\modules\recaptcha.php LN202
486
+ *
487
+ * return void
488
+ */
489
+ private function __construct() {
490
+ $this->sitekeys = WPCF7::get_option( 'recaptcha' );
491
+ }
492
+
493
+
494
+ /**
495
+ * Singleton
496
+ *
497
+ * The following method was not written by IQComputing and is included in
498
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
499
+ * contact-form-7\modules\recaptcha.php LN10
500
+ *
501
+ * @return IQFix_ReCaptcha
502
+ */
503
+ public static function get_instance() {
504
+ if ( empty( self::$instance ) ) {
505
+ self::$instance = new self;
506
+ }
507
+
508
+ return self::$instance;
509
+ }
510
+
511
+
512
+ /**
513
+ * Check if reCaptcha is active
514
+ *
515
+ * The following method was not written by IQComputing and is included in
516
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
517
+ * contact-form-7\modules\recaptcha.php LN26
518
+ *
519
+ * @return Boolean
520
+ */
521
+ public function is_active() {
522
+ $sitekey = $this->get_sitekey();
523
+ $secret = $this->get_secret( $sitekey );
524
+ return $sitekey && $secret;
525
+ }
526
+
527
+
528
+ /**
529
+ * Get set reCaptcha sitekey
530
+ *
531
+ * The following method was not written by IQComputing and is included in
532
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
533
+ * contact-form-7\modules\recaptcha.php LN45
534
+ *
535
+ * @return String|FALSE
536
+ */
537
+ public function get_sitekey() {
538
+ if ( empty( $this->sitekeys )
539
+ or ! is_array( $this->sitekeys ) ) {
540
+ return false;
541
+ }
542
+
543
+ $sitekeys = array_keys( $this->sitekeys );
544
+
545
+ return $sitekeys[0];
546
+ }
547
+
548
+
549
+ /**
550
+ * Get set reCaptcha secret key
551
+ *
552
+ * The following method was not written by IQComputing and is included in
553
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
554
+ * contact-form-7\modules\recaptcha.php LN55
555
+ *
556
+ * @return String|FALSE
557
+ */
558
+ public function get_secret( $sitekey ) {
559
+ $sitekeys = (array) $this->sitekeys;
560
+
561
+ if ( isset( $sitekeys[$sitekey] ) ) {
562
+ return $sitekeys[$sitekey];
563
+ } else {
564
+ return false;
565
+ }
566
+ }
567
+
568
+
569
+ /**
570
+ * Verify reCaptcha Response
571
+ *
572
+ * The following method was not written by IQComputing and is included in
573
+ * Contact Form 7 v5.0.5 by Takayuki Miyoshi
574
+ * contact-form-7\modules\recaptcha.php LN65
575
+ *
576
+ * @param String $response_token
577
+ *
578
+ * @return Boolean $is_human
579
+ */
580
+ public function verify( $response_token ) {
581
+
582
+ $is_human = false;
583
+
584
+ if ( empty( $response_token ) ) {
585
+ return $is_human;
586
+ }
587
+
588
+ $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
589
+ $source = IQFix_WPCF7_Deity::verify_recaptcha_source( $source );
590
+ $url = sprintf( 'https://www.%s/recaptcha/api/siteverify', $source );
591
+ $sitekey = $this->get_sitekey();
592
+ $secret = $this->get_secret( $sitekey );
593
+
594
+ $response = wp_safe_remote_post( $url, array(
595
+ 'body' => array(
596
+ 'secret' => $secret,
597
+ 'response' => $response_token,
598
+ 'remoteip' => $_SERVER['REMOTE_ADDR'],
599
+ ),
600
+ ) );
601
+
602
+ if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
603
+
604
+ if ( WP_DEBUG ) {
605
+ $this->log( $endpoint, $request, $response );
606
+ }
607
+
608
+ return $is_human;
609
+
610
+ }
611
+
612
+ $response = wp_remote_retrieve_body( $response );
613
+ $response = json_decode( $response, true );
614
+ $is_human = isset( $response['success'] ) && true == $response['success'];
615
+
616
+ return apply_filters( 'wpcf7_recaptcha_verify_response', $is_human, $response );
617
+
618
+ }
619
+
620
+ }
621
+
622
+ }
623
+ add_action( 'init', 'iqfix_recaptcha_class_init', 11 );
624
+
625
+
626
+ /**
627
+ * Add some inline CSS for the reCaptcha iframe
628
+ *
629
+ * @return void
630
+ */
631
+ function iqfix_recaptcha_inline_css() {
632
+
633
+ $iqfix_css ='.wpcf7 .wpcf7-recaptcha iframe {margin-bottom: 0;}';
634
+ wp_add_inline_style( 'contact-form-7', $iqfix_css );
635
+
636
+ }
 
637
  add_action( 'wp_enqueue_scripts', 'iqfix_recaptcha_inline_css' );
wpcf7-recaptcha.php CHANGED
@@ -1,407 +1,407 @@
1
- <?php
2
- /**
3
- * Plugin Name: Contact Form 7 - reCaptcha v2
4
- * Description: ReCaptcha v2 Fix for Contact Form 7 5.1 and later.
5
- * Version: 1.1.9
6
- * Author: IQComputing
7
- * Author URI: http://www.iqcomputing.com/
8
- * License: GPL2
9
- * Text Domain: wpcf7-recaptcha
10
- */
11
-
12
-
13
- defined( 'ABSPATH' ) or die( 'You cannot be here.' );
14
-
15
-
16
- /**
17
- * IQComputing Contact Form 7 reCaptcha Fix, Deity Class
18
- */
19
- Class IQFix_WPCF7_Deity {
20
-
21
- public static $version = '1.1.9';
22
-
23
-
24
- /**
25
- * Class Registration, set up necessities
26
- *
27
- * @return void
28
- */
29
- public static function register() {
30
-
31
- $class = new self();
32
- $class->include_files();
33
- $class->action_hooks();
34
- $class->filter_hooks();
35
-
36
- }
37
-
38
-
39
- /**
40
- * Really don't like dynamically assigning URLs by user saved options.
41
- * This method will verify at every stage that the given value is either
42
- * `google.com` or `recaptcha.net`
43
- * Nowhere in between can this value be changed to anything but those two.
44
- *
45
- * @param $expectation
46
- *
47
- * @return $reality
48
- */
49
- public static function verify_recaptcha_source( $expectation = 'google.com' ) {
50
-
51
- $reality = ( in_array( $expectation, array( 'google.com', 'recaptcha.net' ) ) ) ? $expectation : 'google.com';
52
-
53
- return $reality;
54
-
55
- }
56
-
57
-
58
- /**
59
- * Include any necessary files
60
- *
61
- * @return void
62
- */
63
- private function include_files() {
64
-
65
- $selection = WPCF7::get_option( 'iqfix_recaptcha' );
66
-
67
- // Prevent update from v2 to v3 notice.
68
- WPCF7::update_option( 'recaptcha_v2_v3_warning', false );
69
-
70
- if( empty( $selection ) || version_compare( WPCF7_VERSION, '5.1', '<' ) ) {
71
- return;
72
- }
73
-
74
- include_once( plugin_dir_path( __FILE__ ) . 'recaptcha-v2.php' );
75
-
76
- if( class_exists( 'Flamingo_Contact' ) ) {
77
- include_once( plugin_dir_path( __FILE__ ) . 'flamingo.php' ); // Flamingo updates
78
- }
79
-
80
- }
81
-
82
-
83
- /**
84
- * Save the reCaptcha settings from our options page
85
- * @see IQFix_WPCF7_Deity::display_recaptcha_version_subpage()
86
- *
87
- * @return Boolean
88
- */
89
- private function save_recaptcha_settings() {
90
-
91
- // Form hasn't POSTed, return early
92
- if( ! isset( $_POST, $_POST['iqfix_recaptcha_version'], $_POST['iqfix_wpcf7_submit'] ) ) {
93
- return false;
94
- }
95
-
96
- // Ensure we have and can verify our nonce. IF not, return early
97
- if( ! ( ! empty( $_POST['iqfix_wpcf7_nonce'] ) && wp_verify_nonce( $_POST['iqfix_wpcf7_nonce'], 'iqfix_wpcf7_vers_select' ) ) ) {
98
- return false;
99
- }
100
-
101
- $selection = intval( $_POST['iqfix_recaptcha_version'] );
102
- $source = ( isset( $_POST, $_POST['iqfix_recaptcha_source'] ) ) ? sanitize_text_field( $_POST['iqfix_recaptcha_source'] ) : 'google.com';
103
- $source = self::verify_recaptcha_source( $source );
104
-
105
- // Save Network Settings
106
- if( is_network_admin() && isset( $_POST['wpcf7_recaptcha_network'] ) ) {
107
-
108
- $sitekey = trim( $_POST['wpcf7_recaptcha_network']['sitekey'] );
109
- $secretkey = trim( $_POST['wpcf7_recaptcha_network']['secretkey'] );
110
-
111
- update_site_option( 'network_iqfix_recaptcha', array(
112
- 'sitekey' => $sitekey,
113
- 'secret' => $secretkey,
114
- 'iqfix_recaptcha' => $selection,
115
- 'recaptcha_source' => $source,
116
- ) );
117
-
118
- // Save Regular WPCF7 Settings
119
- } else {
120
-
121
- WPCF7::update_option( 'iqfix_recaptcha', $selection );
122
- WPCF7::update_option( 'iqfix_recaptcha_source', $source );
123
-
124
- }
125
-
126
- return true;
127
-
128
- }
129
-
130
-
131
- /**
132
- * Add any necessary action hooks
133
- *
134
- * @return void
135
- */
136
- private function action_hooks() {
137
-
138
- add_action( 'admin_menu', array( $this, 'register_submenus' ) );
139
- add_action( 'network_admin_menu', array( $this, 'register_network_submenus' ) );
140
-
141
- }
142
-
143
-
144
- /**
145
- * Add any necessary filter hooks
146
- *
147
- * @return void
148
- */
149
- private function filter_hooks() {
150
-
151
- add_filter( 'option_wpcf7', array( $this, 'network_wpcf7_options' ), 9 );
152
-
153
- }
154
-
155
-
156
- /**
157
- * Register submenus for picking ReCaptcha versions
158
- *
159
- * @return void
160
- */
161
- public function register_submenus() {
162
-
163
- add_submenu_page(
164
- 'wpcf7',
165
- esc_html__( 'reCaptcha Version', 'wpcf7-recaptcha' ),
166
- esc_html__( 'reCaptcha Version', 'wpcf7-recaptcha' ),
167
- WPCF7_ADMIN_READ_WRITE_CAPABILITY,
168
- 'recaptcha-version',
169
- array( $this, 'display_recaptcha_version_subpage' )
170
- );
171
-
172
- }
173
-
174
-
175
- /**
176
- * Register submenus for the Network Admin Panel
177
- *
178
- * @return void
179
- */
180
- public function register_network_submenus() {
181
-
182
- add_submenu_page(
183
- 'plugins.php',
184
- esc_html__( 'WPCF7 reCaptcha Settings', 'wpcf7-recaptcha' ),
185
- esc_html__( 'WPCF7 reCaptcha Settings', 'wpcf7-recaptcha' ),
186
- 'manage_network_plugins',
187
- 'recaptcha-version',
188
- array( $this, 'display_recaptcha_version_subpage' )
189
- );
190
-
191
- }
192
-
193
-
194
- /**
195
- * Display reCaptcha version subpage
196
- *
197
- * @return void
198
- */
199
- public function display_recaptcha_version_subpage() {
200
-
201
- $updated = $this->save_recaptcha_settings();
202
-
203
- // Grab Network Settings
204
- if( is_network_admin() ) {
205
-
206
- $network_options = get_site_option( 'network_iqfix_recaptcha' );
207
- $selection = ( ! empty( $network_options['iqfix_recaptcha'] ) ) ? $network_options['iqfix_recaptcha'] : '';
208
- $source = ( ! empty( $network_options['recaptcha_source'] ) ) ? $network_options['recaptcha_source'] : '';
209
- $sitekey = ( ! empty( $network_options['sitekey'] ) ) ? $network_options['sitekey'] : '';
210
- $secretkey = ( ! empty( $network_options['secret'] ) ) ? $network_options['secret'] : '';
211
-
212
- // Grab Site Settings
213
- } else {
214
-
215
- $selection = WPCF7::get_option( 'iqfix_recaptcha' );
216
- $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
217
-
218
- }
219
-
220
- // Show simple message
221
- if( version_compare( WPCF7_VERSION, '5.1', '<' ) ) {
222
- printf(
223
- '<div class="wrap"><h1>%1$s</h1><p>%2$s</p></div>',
224
- esc_html__( 'Contact Form 7 - reCaptcha v2', 'wpcf7-recaptcha' ),
225
- esc_html__( 'This version of Contact Form 7 already uses reCaptcha version 2, you do not need \'Contact Form 7 - reCaptcha v2\' installed at this time.', 'wpcf7-recaptcha' )
226
- );
227
- return;
228
- }
229
-
230
- ?>
231
-
232
- <div class="wrap">
233
- <style>
234
- #iqFacebook {margin-top: 40px;}
235
- #iqFacebook a {display: inline-block; margin-bottom: 12px;}
236
- #iqFacebook img {display: block; max-width: 100%; height: auto;}
237
- </style>
238
-
239
- <?php
240
-
241
- printf( '<h1>%1$s</h1>', esc_html__( 'Contact Form 7 - reCaptcha v2', 'wpcf7-recaptcha' ) );
242
-
243
- if( $updated ) {
244
- printf(
245
- '<div class="notice updated"><p>%1$s</p></div>',
246
- esc_html__( 'Your reCaptcha settings have been updated.', 'wpcf7-recaptcha' )
247
- );
248
- }
249
-
250
- /* translators: %s is a shortcode example wrapped in <code> tags. */
251
- printf( '<p>%1$s</p>',
252
- sprintf( esc_html__( 'Select the version of reCaptcha you would like to use. You will still need to use the %s shortcode tag in your Contact Form 7 forms.', 'wpcf7-recaptcha' ),
253
- '<code>[recaptcha]</code>'
254
- )
255
- );
256
- ?>
257
-
258
- <form method="post">
259
- <?php wp_nonce_field( 'iqfix_wpcf7_vers_select', 'iqfix_wpcf7_nonce' ); ?>
260
-
261
- <label for="iqfix_recaptcha_version"><strong><?php esc_html_e( 'Select reCaptcha Usage' ); ?>:</strong></label><br />
262
- <select id="iqfix_recaptcha_version" name="iqfix_recaptcha_version">
263
- <option value="0"><?php esc_html_e( 'Default Usage', 'wpcf7-recaptcha' ); ?></option>
264
- <option value="2" <?php selected( $selection, 2, true ); ?>><?php esc_html_e( 'reCaptcha Version 2', 'wpcf7-recaptcha' ); ?></option>
265
- </select>
266
-
267
- <?php printf( '<p>%s</p>', esc_html__( 'If you\'re not sure if your country blocks Google then you may leave this as default. Otherwise, if your country blocks google.com requests then please select the suggested recaptcha.net alternative below.' ) ); ?>
268
-
269
- <label for="iqfix_recaptcha_source"><strong><?php esc_html_e( 'Select reCaptcha Source' ); ?>:</strong></label><br />
270
- <select id="iqfix_recaptcha_source" name="iqfix_recaptcha_source">
271
- <option value="google.com">google.com</option>
272
- <option value="recaptcha.net" <?php selected( $source, 'recaptcha.net', true ); ?>>recaptcha.net</option>
273
- </select>
274
-
275
- <?php if( is_network_admin() ) : ?>
276
-
277
- <hr />
278
- <h2><?php esc_html__( 'Network Wide Settings' ); ?></h2>
279
-
280
- <p><strong><?php _e( 'Please read all of the below before committing to these changes.', 'wpcf7-recaptcha' ); ?></strong></p>
281
- <p><?php _e( 'You may set Network wide API keys below. Please ensure that every network site is whitelisted in the Google API Console. ReCaptcha keys can still be set ( or unset ) on a per site basis if necessary.', 'wpcf7-recaptcha' ); ?></p>
282
- <p><?php
283
- /* translators: Care for HTML in string used for emphasis. */
284
- _e( 'Do note that these keys will automatically apply to all network websites upon save <strong>if keys are not detected</strong>. If some network websites use reCaptcha v3 please <u>do not use this option</u> and set it on a per site level.', 'wpcf7-recaptcha' );
285
- ?></p>
286
-
287
- <table class="form-table">
288
- <tbody>
289
- <tr>
290
- <th><?php _e( 'Site Key', 'wpcf7-recaptcha' ); ?></th>
291
- <td><input type="text" name="wpcf7_recaptcha_network[sitekey]" class="regular-text" value="<?php echo esc_attr( $sitekey ); ?>" /></td>
292
- </tr>
293
- <tr>
294
- <th><?php _e( 'Secret Key', 'wpcf7-recaptcha' ); ?></th>
295
- <td><input type="password" name="wpcf7_recaptcha_network[secretkey]" class="regular-text" value="<?php echo esc_attr( $secretkey ); ?>" /></td>
296
- </tr>
297
- </tbody>
298
- </table>
299
-
300
- <?php endif; ?>
301
-
302
- <?php submit_button( esc_html__( 'Submit', 'wpcf7-recaptcha' ), 'submit', 'iqfix_wpcf7_submit' ); ?>
303
- </form>
304
-
305
- <div id="iqFacebook">
306
- <?php
307
- printf( '<a href="%1$s" target="_blank"><img src="%2$s" width="540" height="410" alt="%3$s" /></a>',
308
- esc_url( 'https://www.facebook.com/iqcomputing' ),
309
- plugins_url( 'assets/images/facebook-like.png', __FILE__ ),
310
- /* translators: Image alternative tag. */
311
- esc_attr__( 'Like IQComputing on Facebook mascot', 'wpcf7-recaptcha' )
312
- );
313
- printf( '<p>%1$s</p>',
314
- esc_html__( 'Click the image above and like us on Facebook! Facebook is where you will receive the latest news on both this plugin and all future IQComputing plugins.', 'wpcf7-recaptcha' )
315
- );
316
- ?>
317
- </div> <!-- id="iqFacebook" -->
318
-
319
- </div>
320
-
321
- <?php
322
-
323
- }
324
-
325
-
326
- /**
327
- * Filter Hook into WPCF7 get option
328
- * Maybe replace it work a network option
329
- *
330
- * @param Mixed $value
331
- * @param Stirng $option_name
332
- *
333
- * @return Mixed $value
334
- */
335
- public function network_wpcf7_options( $value ) {
336
-
337
- // If we're not on a multisite setup we can skip this filter hook
338
- if( ! is_multisite() || empty( $value ) ) {
339
- return $value;
340
- }
341
-
342
- // Allow sites to be setup regardless of network specifciations
343
- if( is_admin()
344
- && isset( $_GET, $_GET['page'], $_GET['service'], $_GET['action'] )
345
- && 'wpcf7-integration' == $_GET['page']
346
- && 'recaptcha' == $_GET['service']
347
- && 'setup' == $_GET['action']
348
- ) {
349
- return $value;
350
- }
351
-
352
- // Grab Network Option
353
- $network_wpcf7 = get_site_option( 'network_iqfix_recaptcha' );
354
- $network_wpcf7 = array_filter( (array)$network_wpcf7 );
355
-
356
- // Set site keys IF there are no API keys set.
357
- if( empty( $value['recaptcha'] ) && ! empty( $network_wpcf7['sitekey'] ) && ! empty( $network_wpcf7['secret'] ) ) {
358
- $value['recaptcha'] = array(
359
- $network_wpcf7['sitekey'] => $network_wpcf7['secret'],
360
- );
361
- }
362
-
363
- // Set IQFix reCaptcha
364
- if( ! isset( $value['iqfix_recaptcha'] ) && ! empty( $network_wpcf7['iqfix_recaptcha'] ) ) {
365
- $value['iqfix_recaptcha'] = $network_wpcf7['iqfix_recaptcha'];
366
- }
367
-
368
- // Set IQFix reCaptcha Source
369
- if( ! isset( $value['iqfix_recaptcha_source'] ) && ! empty( $network_wpcf7['recaptcha_source'] ) ) {
370
- $value['iqfix_recaptcha_source'] = $network_wpcf7['recaptcha_source'];
371
- }
372
-
373
- return $value;
374
-
375
- }
376
-
377
-
378
- } // END Class IQFix_WPCF7_Deity
379
-
380
-
381
- /**
382
- * Initialize Class
383
- *
384
- * @return void
385
- */
386
- function iqfix_wpcf7_deity_init() {
387
-
388
- if( class_exists( 'WPCF7' ) ) {
389
- IQFix_WPCF7_Deity::register();
390
- }
391
-
392
- }
393
- add_action( 'plugins_loaded', 'iqfix_wpcf7_deity_init' );
394
-
395
-
396
- /**
397
- * Remove upgrade notice from v2 to v3
398
- * Prevent api keys from being reset.
399
- *
400
- * @return void
401
- */
402
- function iqfix_wpcf7_upgrade_recaptcha_v2_v3_removal() {
403
-
404
- remove_action( 'wpcf7_upgrade', 'wpcf7_upgrade_recaptcha_v2_v3', 10 );
405
-
406
- }
407
  add_action( 'admin_init', 'iqfix_wpcf7_upgrade_recaptcha_v2_v3_removal', 9 );
1
+ <?php
2
+ /**
3
+ * Plugin Name: Contact Form 7 - reCaptcha v2
4
+ * Description: ReCaptcha v2 Fix for Contact Form 7 5.1 and later.
5
+ * Version: 1.2.0
6
+ * Author: IQComputing
7
+ * Author URI: http://www.iqcomputing.com/
8
+ * License: GPL2
9
+ * Text Domain: wpcf7-recaptcha
10
+ */
11
+
12
+
13
+ defined( 'ABSPATH' ) or die( 'You cannot be here.' );
14
+
15
+
16
+ /**
17
+ * IQComputing Contact Form 7 reCaptcha Fix, Deity Class
18
+ */
19
+ Class IQFix_WPCF7_Deity {
20
+
21
+ public static $version = '1.2.0';
22
+
23
+
24
+ /**
25
+ * Class Registration, set up necessities
26
+ *
27
+ * @return void
28
+ */
29
+ public static function register() {
30
+
31
+ $class = new self();
32
+ $class->include_files();
33
+ $class->action_hooks();
34
+ $class->filter_hooks();
35
+
36
+ }
37
+
38
+
39
+ /**
40
+ * Really don't like dynamically assigning URLs by user saved options.
41
+ * This method will verify at every stage that the given value is either
42
+ * `google.com` or `recaptcha.net`
43
+ * Nowhere in between can this value be changed to anything but those two.
44
+ *
45
+ * @param $expectation
46
+ *
47
+ * @return $reality
48
+ */
49
+ public static function verify_recaptcha_source( $expectation = 'google.com' ) {
50
+
51
+ $reality = ( in_array( $expectation, array( 'google.com', 'recaptcha.net' ) ) ) ? $expectation : 'google.com';
52
+
53
+ return $reality;
54
+
55
+ }
56
+
57
+
58
+ /**
59
+ * Include any necessary files
60
+ *
61
+ * @return void
62
+ */
63
+ private function include_files() {
64
+
65
+ $selection = WPCF7::get_option( 'iqfix_recaptcha' );
66
+
67
+ // Prevent update from v2 to v3 notice.
68
+ WPCF7::update_option( 'recaptcha_v2_v3_warning', false );
69
+
70
+ if( empty( $selection ) || version_compare( WPCF7_VERSION, '5.1', '<' ) ) {
71
+ return;
72
+ }
73
+
74
+ include_once( plugin_dir_path( __FILE__ ) . 'recaptcha-v2.php' );
75
+
76
+ if( class_exists( 'Flamingo_Contact' ) ) {
77
+ include_once( plugin_dir_path( __FILE__ ) . 'flamingo.php' ); // Flamingo updates
78
+ }
79
+
80
+ }
81
+
82
+
83
+ /**
84
+ * Save the reCaptcha settings from our options page
85
+ * @see IQFix_WPCF7_Deity::display_recaptcha_version_subpage()
86
+ *
87
+ * @return Boolean
88
+ */
89
+ private function save_recaptcha_settings() {
90
+
91
+ // Form hasn't POSTed, return early
92
+ if( ! isset( $_POST, $_POST['iqfix_recaptcha_version'], $_POST['iqfix_wpcf7_submit'] ) ) {
93
+ return false;
94
+ }
95
+
96
+ // Ensure we have and can verify our nonce. IF not, return early
97
+ if( ! ( ! empty( $_POST['iqfix_wpcf7_nonce'] ) && wp_verify_nonce( $_POST['iqfix_wpcf7_nonce'], 'iqfix_wpcf7_vers_select' ) ) ) {
98
+ return false;
99
+ }
100
+
101
+ $selection = intval( $_POST['iqfix_recaptcha_version'] );
102
+ $source = ( isset( $_POST, $_POST['iqfix_recaptcha_source'] ) ) ? sanitize_text_field( $_POST['iqfix_recaptcha_source'] ) : 'google.com';
103
+ $source = self::verify_recaptcha_source( $source );
104
+
105
+ // Save Network Settings
106
+ if( is_network_admin() && isset( $_POST['wpcf7_recaptcha_network'] ) ) {
107
+
108
+ $sitekey = trim( $_POST['wpcf7_recaptcha_network']['sitekey'] );
109
+ $secretkey = trim( $_POST['wpcf7_recaptcha_network']['secretkey'] );
110
+
111
+ update_site_option( 'network_iqfix_recaptcha', array(
112
+ 'sitekey' => $sitekey,
113
+ 'secret' => $secretkey,
114
+ 'iqfix_recaptcha' => $selection,
115
+ 'recaptcha_source' => $source,
116
+ ) );
117
+
118
+ // Save Regular WPCF7 Settings
119
+ } else {
120
+
121
+ WPCF7::update_option( 'iqfix_recaptcha', $selection );
122
+ WPCF7::update_option( 'iqfix_recaptcha_source', $source );
123
+
124
+ }
125
+
126
+ return true;
127
+
128
+ }
129
+
130
+
131
+ /**
132
+ * Add any necessary action hooks
133
+ *
134
+ * @return void
135
+ */
136
+ private function action_hooks() {
137
+
138
+ add_action( 'admin_menu', array( $this, 'register_submenus' ) );
139
+ add_action( 'network_admin_menu', array( $this, 'register_network_submenus' ) );
140
+
141
+ }
142
+
143
+
144
+ /**
145
+ * Add any necessary filter hooks
146
+ *
147
+ * @return void
148
+ */
149
+ private function filter_hooks() {
150
+
151
+ add_filter( 'option_wpcf7', array( $this, 'network_wpcf7_options' ), 9 );
152
+
153
+ }
154
+
155
+
156
+ /**
157
+ * Register submenus for picking ReCaptcha versions
158
+ *
159
+ * @return void
160
+ */
161
+ public function register_submenus() {
162
+
163
+ add_submenu_page(
164
+ 'wpcf7',
165
+ esc_html__( 'reCaptcha Version', 'wpcf7-recaptcha' ),
166
+ esc_html__( 'reCaptcha Version', 'wpcf7-recaptcha' ),
167
+ WPCF7_ADMIN_READ_WRITE_CAPABILITY,
168
+ 'recaptcha-version',
169
+ array( $this, 'display_recaptcha_version_subpage' )
170
+ );
171
+
172
+ }
173
+
174
+
175
+ /**
176
+ * Register submenus for the Network Admin Panel
177
+ *
178
+ * @return void
179
+ */
180
+ public function register_network_submenus() {
181
+
182
+ add_submenu_page(
183
+ 'plugins.php',
184
+ esc_html__( 'WPCF7 reCaptcha Settings', 'wpcf7-recaptcha' ),
185
+ esc_html__( 'WPCF7 reCaptcha Settings', 'wpcf7-recaptcha' ),
186
+ 'manage_network_plugins',
187
+ 'recaptcha-version',
188
+ array( $this, 'display_recaptcha_version_subpage' )
189
+ );
190
+
191
+ }
192
+
193
+
194
+ /**
195
+ * Display reCaptcha version subpage
196
+ *
197
+ * @return void
198
+ */
199
+ public function display_recaptcha_version_subpage() {
200
+
201
+ $updated = $this->save_recaptcha_settings();
202
+
203
+ // Grab Network Settings
204
+ if( is_network_admin() ) {
205
+
206
+ $network_options = get_site_option( 'network_iqfix_recaptcha' );
207
+ $selection = ( ! empty( $network_options['iqfix_recaptcha'] ) ) ? $network_options['iqfix_recaptcha'] : '';
208
+ $source = ( ! empty( $network_options['recaptcha_source'] ) ) ? $network_options['recaptcha_source'] : '';
209
+ $sitekey = ( ! empty( $network_options['sitekey'] ) ) ? $network_options['sitekey'] : '';
210
+ $secretkey = ( ! empty( $network_options['secret'] ) ) ? $network_options['secret'] : '';
211
+
212
+ // Grab Site Settings
213
+ } else {
214
+
215
+ $selection = WPCF7::get_option( 'iqfix_recaptcha' );
216
+ $source = WPCF7::get_option( 'iqfix_recaptcha_source' );
217
+
218
+ }
219
+
220
+ // Show simple message
221
+ if( version_compare( WPCF7_VERSION, '5.1', '<' ) ) {
222
+ printf(
223
+ '<div class="wrap"><h1>%1$s</h1><p>%2$s</p></div>',
224
+ esc_html__( 'Contact Form 7 - reCaptcha v2', 'wpcf7-recaptcha' ),
225
+ esc_html__( 'This version of Contact Form 7 already uses reCaptcha version 2, you do not need \'Contact Form 7 - reCaptcha v2\' installed at this time.', 'wpcf7-recaptcha' )
226
+ );
227
+ return;
228
+ }
229
+
230
+ ?>
231
+
232
+ <div class="wrap">
233
+ <style>
234
+ #iqFacebook {margin-top: 40px;}
235
+ #iqFacebook a {display: inline-block; margin-bottom: 12px;}
236
+ #iqFacebook img {display: block; max-width: 100%; height: auto;}
237
+ </style>
238
+
239
+ <?php
240
+
241
+ printf( '<h1>%1$s</h1>', esc_html__( 'Contact Form 7 - reCaptcha v2', 'wpcf7-recaptcha' ) );
242
+
243
+ if( $updated ) {
244
+ printf(
245
+ '<div class="notice updated"><p>%1$s</p></div>',
246
+ esc_html__( 'Your reCaptcha settings have been updated.', 'wpcf7-recaptcha' )
247
+ );
248
+ }
249
+
250
+ /* translators: %s is a shortcode example wrapped in <code> tags. */
251
+ printf( '<p>%1$s</p>',
252
+ sprintf( esc_html__( 'Select the version of reCaptcha you would like to use. You will still need to use the %s shortcode tag in your Contact Form 7 forms.', 'wpcf7-recaptcha' ),
253
+ '<code>[recaptcha]</code>'
254
+ )
255
+ );
256
+ ?>
257
+
258
+ <form method="post">
259
+ <?php wp_nonce_field( 'iqfix_wpcf7_vers_select', 'iqfix_wpcf7_nonce' ); ?>
260
+
261
+ <label for="iqfix_recaptcha_version"><strong><?php esc_html_e( 'Select reCaptcha Usage' ); ?>:</strong></label><br />
262
+ <select id="iqfix_recaptcha_version" name="iqfix_recaptcha_version">
263
+ <option value="0"><?php esc_html_e( 'Default Usage', 'wpcf7-recaptcha' ); ?></option>
264
+ <option value="2" <?php selected( $selection, 2, true ); ?>><?php esc_html_e( 'reCaptcha Version 2', 'wpcf7-recaptcha' ); ?></option>
265
+ </select>
266
+
267
+ <?php printf( '<p>%s</p>', esc_html__( 'If you\'re not sure if your country blocks Google then you may leave this as default. Otherwise, if your country blocks google.com requests then please select the suggested recaptcha.net alternative below.' ) ); ?>
268
+
269
+ <label for="iqfix_recaptcha_source"><strong><?php esc_html_e( 'Select reCaptcha Source' ); ?>:</strong></label><br />
270
+ <select id="iqfix_recaptcha_source" name="iqfix_recaptcha_source">
271
+ <option value="google.com">google.com</option>
272
+ <option value="recaptcha.net" <?php selected( $source, 'recaptcha.net', true ); ?>>recaptcha.net</option>
273
+ </select>
274
+
275
+ <?php if( is_network_admin() ) : ?>
276
+
277
+ <hr />
278
+ <h2><?php esc_html__( 'Network Wide Settings' ); ?></h2>
279
+
280
+ <p><strong><?php _e( 'Please read all of the below before committing to these changes.', 'wpcf7-recaptcha' ); ?></strong></p>
281
+ <p><?php _e( 'You may set Network wide API keys below. Please ensure that every network site is whitelisted in the Google API Console. ReCaptcha keys can still be set ( or unset ) on a per site basis if necessary.', 'wpcf7-recaptcha' ); ?></p>
282
+ <p><?php
283
+ /* translators: Care for HTML in string used for emphasis. */
284
+ _e( 'Do note that these keys will automatically apply to all network websites upon save <strong>if keys are not detected</strong>. If some network websites use reCaptcha v3 please <u>do not use this option</u> and set it on a per site level.', 'wpcf7-recaptcha' );
285
+ ?></p>
286
+
287
+ <table class="form-table">
288
+ <tbody>
289
+ <tr>
290
+ <th><?php _e( 'Site Key', 'wpcf7-recaptcha' ); ?></th>
291
+ <td><input type="text" name="wpcf7_recaptcha_network[sitekey]" class="regular-text" value="<?php echo esc_attr( $sitekey ); ?>" /></td>
292
+ </tr>
293
+ <tr>
294
+ <th><?php _e( 'Secret Key', 'wpcf7-recaptcha' ); ?></th>
295
+ <td><input type="password" name="wpcf7_recaptcha_network[secretkey]" class="regular-text" value="<?php echo esc_attr( $secretkey ); ?>" /></td>
296
+ </tr>
297
+ </tbody>
298
+ </table>
299
+
300
+ <?php endif; ?>
301
+
302
+ <?php submit_button( esc_html__( 'Submit', 'wpcf7-recaptcha' ), 'submit', 'iqfix_wpcf7_submit' ); ?>
303
+ </form>
304
+
305
+ <div id="iqFacebook">
306
+ <?php
307
+ printf( '<a href="%1$s" target="_blank"><img src="%2$s" width="540" height="410" alt="%3$s" /></a>',
308
+ esc_url( 'https://www.facebook.com/iqcomputing' ),
309
+ plugins_url( 'assets/images/facebook-like.png', __FILE__ ),
310
+ /* translators: Image alternative tag. */
311
+ esc_attr__( 'Like IQComputing on Facebook mascot', 'wpcf7-recaptcha' )
312
+ );
313
+ printf( '<p>%1$s</p>',
314
+ esc_html__( 'Click the image above and like us on Facebook! Facebook is where you will receive the latest news on both this plugin and all future IQComputing plugins.', 'wpcf7-recaptcha' )
315
+ );
316
+ ?>
317
+ </div> <!-- id="iqFacebook" -->
318
+
319
+ </div>
320
+
321
+ <?php
322
+
323
+ }
324
+
325
+
326
+ /**
327
+ * Filter Hook into WPCF7 get option
328
+ * Maybe replace it work a network option
329
+ *
330
+ * @param Mixed $value
331
+ * @param Stirng $option_name
332
+ *
333
+ * @return Mixed $value
334
+ */
335
+ public function network_wpcf7_options( $value ) {
336
+
337
+ // If we're not on a multisite setup we can skip this filter hook
338
+ if( ! is_multisite() || empty( $value ) ) {
339
+ return $value;
340
+ }
341
+
342
+ // Allow sites to be setup regardless of network specifciations
343
+ if( is_admin()
344
+ && isset( $_GET, $_GET['page'], $_GET['service'], $_GET['action'] )
345
+ && 'wpcf7-integration' == $_GET['page']
346
+ && 'recaptcha' == $_GET['service']
347
+ && 'setup' == $_GET['action']
348
+ ) {
349
+ return $value;
350
+ }
351
+
352
+ // Grab Network Option
353
+ $network_wpcf7 = get_site_option( 'network_iqfix_recaptcha' );
354
+ $network_wpcf7 = array_filter( (array)$network_wpcf7 );
355
+
356
+ // Set site keys IF there are no API keys set.
357
+ if( empty( $value['recaptcha'] ) && ! empty( $network_wpcf7['sitekey'] ) && ! empty( $network_wpcf7['secret'] ) ) {
358
+ $value['recaptcha'] = array(
359
+ $network_wpcf7['sitekey'] => $network_wpcf7['secret'],
360
+ );
361
+ }
362
+
363
+ // Set IQFix reCaptcha
364
+ if( ! isset( $value['iqfix_recaptcha'] ) && ! empty( $network_wpcf7['iqfix_recaptcha'] ) ) {
365
+ $value['iqfix_recaptcha'] = $network_wpcf7['iqfix_recaptcha'];
366
+ }
367
+
368
+ // Set IQFix reCaptcha Source
369
+ if( ! isset( $value['iqfix_recaptcha_source'] ) && ! empty( $network_wpcf7['recaptcha_source'] ) ) {
370
+ $value['iqfix_recaptcha_source'] = $network_wpcf7['recaptcha_source'];
371
+ }
372
+
373
+ return $value;
374
+
375
+ }
376
+
377
+
378
+ } // END Class IQFix_WPCF7_Deity
379
+
380
+
381
+ /**
382
+ * Initialize Class
383
+ *
384
+ * @return void
385
+ */
386
+ function iqfix_wpcf7_deity_init() {
387
+
388
+ if( class_exists( 'WPCF7' ) ) {
389
+ IQFix_WPCF7_Deity::register();
390
+ }
391
+
392
+ }
393
+ add_action( 'plugins_loaded', 'iqfix_wpcf7_deity_init' );
394
+
395
+
396
+ /**
397
+ * Remove upgrade notice from v2 to v3
398
+ * Prevent api keys from being reset.
399
+ *
400
+ * @return void
401
+ */
402
+ function iqfix_wpcf7_upgrade_recaptcha_v2_v3_removal() {
403
+
404
+ remove_action( 'wpcf7_upgrade', 'wpcf7_upgrade_recaptcha_v2_v3', 10 );
405
+
406
+ }
407
  add_action( 'admin_init', 'iqfix_wpcf7_upgrade_recaptcha_v2_v3_removal', 9 );