Contact Form 7 Captcha - Version 0.0.2

Version Description

Download this release

Release Info

Developer 247wd
Plugin Icon wp plugin Contact Form 7 Captcha
Version 0.0.2
Comparing to
See all releases

Version 0.0.2

Files changed (3) hide show
  1. contact-form-7-simple-recaptcha.php +171 -0
  2. index.php +1 -0
  3. readme.txt +27 -0
contact-form-7-simple-recaptcha.php ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Contact Form 7 Captcha
4
+ Description: Add No CAPTCHA reCAPTCHA to Contact Form 7 using [cf7sr-simple-recaptcha] shortcode
5
+ Version: 0.0.2
6
+ Author: 247wd
7
+ */
8
+
9
+ $cf7sr_key = get_option('cf7sr_key');
10
+ $cf7sr_secret = get_option( 'cf7sr_secret' );
11
+ if (!empty($cf7sr_key) && !empty($cf7sr_secret) && !is_admin()) {
12
+ function enqueue_cf7sr_script() {
13
+ global $cf7sr;
14
+ if (!$cf7sr) {
15
+ return;
16
+ }
17
+ $cf7sr_script_url = 'https://www.google.com/recaptcha/api.js?onload=cf7srLoadCallback&render=explicit';
18
+ $cf7sr_key = get_option( 'cf7sr_key' );
19
+ ?>
20
+ <script type="text/javascript">
21
+ var widgetIds = [];
22
+ var cf7srLoadCallback = function() {
23
+ var cf7srWidgets = document.querySelectorAll('.cf7sr-g-recaptcha');
24
+ for (var i = 0; i < cf7srWidgets.length; ++i) {
25
+ var cf7srWidget = cf7srWidgets[i];
26
+ var widgetId = grecaptcha.render(cf7srWidget.id, {
27
+ 'sitekey' : '<?php echo $cf7sr_key; ?>'
28
+ });
29
+ widgetIds.push(widgetId);
30
+ }
31
+ };
32
+ (function($) {
33
+ $('.wpcf7').on('invalid.wpcf7 mailsent.wpcf7', function() {
34
+ for (var i = 0; i < widgetIds.length; i++) {
35
+ grecaptcha.reset(widgetIds[i]);
36
+ }
37
+ });
38
+ })(jQuery);
39
+ </script>
40
+ <script src="<?php echo $cf7sr_script_url; ?>" async defer></script>
41
+ <?php
42
+ }
43
+ add_action('wp_footer', 'enqueue_cf7sr_script');
44
+
45
+ function cf7sr_wpcf7_form_elements($form) {
46
+ $form = do_shortcode($form);
47
+ return $form;
48
+ }
49
+ add_filter('wpcf7_form_elements', 'cf7sr_wpcf7_form_elements');
50
+
51
+ function cf7sr_shortcode($atts) {
52
+ global $cf7sr;
53
+ $cf7sr = true;
54
+ $cf7sr_key = get_option('cf7sr_key');
55
+ return '<div id="cf7sr-' . uniqid() . '" class="cf7sr-g-recaptcha" data-sitekey="' . $cf7sr_key
56
+ . '"></div><span class="wpcf7-form-control-wrap cf7sr-g-recaptcha-invalid"></span>';
57
+ }
58
+ add_shortcode('cf7sr-simple-recaptcha', 'cf7sr_shortcode');
59
+
60
+ function cf7sr_verify_recaptcha($result) {
61
+ $_wpcf7 = ! empty($_POST['_wpcf7']) ? absint($_POST['_wpcf7']) : 0;
62
+ if (empty($_wpcf7)) {
63
+ return $result;
64
+ }
65
+
66
+ $submission = WPCF7_Submission::get_instance();
67
+ $data = $submission->get_posted_data();
68
+ if (empty($data['_wpcf7'])) {
69
+ return $result;
70
+ }
71
+
72
+ $cf7_text = do_shortcode( '[contact-form-7 id="' . $data['_wpcf7'] . '"]' );
73
+ $cf7sr_key = get_option( 'cf7sr_key' );
74
+ if (false === strpos($cf7_text, $cf7sr_key)) {
75
+ return $result;
76
+ }
77
+
78
+ $message = get_option('cf7sr_message');
79
+ if (empty($message)) {
80
+ $message = 'Invalid captcha';
81
+ }
82
+
83
+ if (empty($data['g-recaptcha-response'])) {
84
+ $result->invalidate(array('type' => 'captcha', 'name' => 'cf7sr-g-recaptcha-invalid'), $message);
85
+ return $result;
86
+ }
87
+
88
+ $cf7sr_secret = get_option('cf7sr_secret');
89
+ $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $cf7sr_secret . '&response=' . $data['g-recaptcha-response'];
90
+ $request = wp_remote_get($url);
91
+ $body = wp_remote_retrieve_body($request);
92
+ $response = json_decode($body);
93
+ if (!(isset ($response->success) && 1 == $response->success)) {
94
+ $result->invalidate(array('type' => 'captcha', 'name' => 'cf7sr-g-recaptcha-invalid'), $message);
95
+ }
96
+
97
+ return $result;
98
+ }
99
+ add_filter('wpcf7_validate', 'cf7sr_verify_recaptcha', 20, 2);
100
+ }
101
+
102
+ if (is_admin()) {
103
+ function cf7sr_add_action_links($links) {
104
+ array_unshift($links , '<a href="' . admin_url( 'options-general.php?page=cf7sr_edit' ) . '">Settings</a>');
105
+ array_unshift($links , '<a target="captcha" style="font-weight: 900; color: #d54e21;" href="http://www.cf7captcha.com">Upgrade To Pro</a>');
106
+ return $links;
107
+ }
108
+ add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'cf7sr_add_action_links', 10, 2 );
109
+
110
+ function cf7sr_adminhtml() {
111
+ if (!current_user_can('manage_options')) {
112
+ wp_die(__('You do not have sufficient permissions to access this page.'));
113
+ }
114
+ if (!empty ($_POST['update'])) {
115
+ $cf7sr_key = !empty ($_POST['cf7sr_key']) ? sanitize_text_field($_POST['cf7sr_key']) : '';
116
+ update_option('cf7sr_key', $cf7sr_key);
117
+
118
+ $cf7sr_secret = !empty ($_POST['cf7sr_secret']) ? sanitize_text_field($_POST['cf7sr_secret']) : '';
119
+ update_option('cf7sr_secret', $cf7sr_secret);
120
+
121
+ $cf7sr_message = !empty ($_POST['cf7sr_message']) ? sanitize_text_field($_POST['cf7sr_message']) : '';
122
+ update_option('cf7sr_message', $cf7sr_message);
123
+
124
+ $updated = 1;
125
+ } else {
126
+ $cf7sr_key = get_option('cf7sr_key');
127
+ $cf7sr_secret = get_option('cf7sr_secret');
128
+ $cf7sr_message = get_option('cf7sr_message');
129
+ }
130
+ ?>
131
+ <div class="cf7sr-wrap" style="font-size: 15px; background: #fff; border: 1px solid #e5e5e5; margin-top: 20px; padding: 20px; margin-right: 20px;">
132
+ <h2>Contact Form 7 Captcha Settings</h2>
133
+ You can generate Site key and Secret key <a target="_blank" href="https://www.google.com/recaptcha/admin/create">here</a>. Once generated, paste them below for this plugin to work.<br>
134
+ To add Recaptcha to CF7 form, add <strong>[cf7sr-simple-recaptcha]</strong> in your form ( preferable above submit button )<br><br>
135
+ <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
136
+ <input type="hidden" value="1" name="update">
137
+ <ul>
138
+ <li><input type="text" style="width: 370px;" value="<?php echo $cf7sr_key; ?>" name="cf7sr_key"> Site key</li>
139
+ <li><input type="text" style="width: 370px;" value="<?php echo $cf7sr_secret; ?>" name="cf7sr_secret"> Secret key</li>
140
+ <li><input type="text" style="width: 370px;" value="<?php echo $cf7sr_message; ?>" name="cf7sr_message"> Invalid captcha error message</li>
141
+ </ul><br>
142
+ <input type="submit" class="button-primary" value="Save Settings">
143
+ </form>
144
+ <?php if (!empty($updated)): ?>
145
+ <p>Settings were updated successfully!</p>
146
+ <?php endif; ?>
147
+ </div>
148
+ <div class="cf7sr-wrap" style="font-size: 15px; background: #fff; border: 1px solid #e5e5e5; margin-top: 20px; padding: 20px; margin-right: 20px;">
149
+ <ul>
150
+ <li><a target="captcha" style="font-weight: 900; color: #d54e21;" href="http://www.cf7captcha.com">CLICK HERE</a> to upgrade to Pro Version and get: <br></li>
151
+ <li>Render captcha widget in a specific language, choose from 70 languages. Free version auto-detects the user's language</li>
152
+ <li>Switch between the color theme of the widget, light or dark</li>
153
+ <li>Switch between the type of the widget, image or audio</li>
154
+ <li>Switch between the size of the widget, normal or compact</li>
155
+ </ul>
156
+ </div>
157
+ <?php
158
+ }
159
+
160
+ function cf7sr_addmenu() {
161
+ add_submenu_page (
162
+ 'options-general.php',
163
+ 'CF7 Simple Recaptcha',
164
+ 'CF7 Simple Recaptcha',
165
+ 'manage_options',
166
+ 'cf7sr_edit',
167
+ 'cf7sr_adminhtml'
168
+ );
169
+ }
170
+ add_action('admin_menu', 'cf7sr_addmenu');
171
+ }
index.php ADDED
@@ -0,0 +1 @@
 
1
+ <?php
readme.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Contact Form 7 Captcha ===
2
+ Contributors: 247wd
3
+ Donate link: http://wordpress.org/plugins/contact-form-7-simple-recaptcha
4
+ Tags: captcha, recaptcha, new recaptcha, contact form 7, no captcha
5
+ Requires at least: 4.1.2
6
+ Tested up to: 4.9.6
7
+ Stable tag: 0.0.2
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Add CAPTCHA to Contact Form 7
12
+
13
+ == Description ==
14
+
15
+ Add Google CAPTCHA to Contact Form 7.<br>
16
+ Protect your Contact Form 7 forms from spam and abuse.<br>
17
+ Can be used to protect multiple forms on same page.<br>
18
+ Tested with Contact Form 7 version 5.0.2 and WordPress version 4.9.6<br>
19
+ Configure plugin from Settings => CF7 Simple Recaptcha.<br>
20
+ After configuration, add [cf7sr-simple-recaptcha] to any Contact Form 7 form.<br>
21
+
22
+ == Installation ==
23
+
24
+ 1. Upload the entire contents of the zip file to your plugin directory
25
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
26
+ 3. Configure plugin from Settings => CF7 Simple Recaptcha
27
+ == Screenshots ==