PluginProbe ʕ •ᴥ•ʔ
ReCaptcha v2 for Contact Form 7 / 1.4.9
ReCaptcha v2 for Contact Form 7 v1.4.9
trunk 1.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wpcf7-recaptcha / wpcf7-recaptcha.php
wpcf7-recaptcha Last commit date
assets 1 year ago changelog.txt 1 year ago flamingo.php 1 year ago license.txt 1 year ago readme.txt 1 year ago recaptcha-v2.php 1 year ago wpcf7-recaptcha.php 1 year ago
wpcf7-recaptcha.php
418 lines
1 <?php
2 /**
3 * Plugin Name: ReCaptcha v2 for Contact Form 7
4 * Description: ReCaptcha v2 Fix for Contact Form 7 5.1 and later.
5 * Version: 1.4.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
22 /**
23 * WPCF7 ReCaptcha Plugin Version
24 *
25 * @var String
26 */
27 public static $version = '1.4.9';
28
29
30 /**
31 * Class Registration, set up necessities
32 *
33 * @return void
34 */
35 public static function register() {
36
37 $class = new self();
38 $class->action_hooks();
39 $class->filter_hooks();
40 $class->include_files();
41
42 }
43
44
45 /**
46 * Really don't like dynamically assigning URLs by user saved options.
47 * This method will verify at every stage that the given value is either
48 * `google.com` or `recaptcha.net`
49 * Nowhere in between can this value be changed to anything but those two.
50 *
51 * @param $expectation
52 *
53 * @return $reality
54 */
55 public static function verify_recaptcha_source( $expectation = 'google.com' ) {
56
57 $reality = ( in_array( $expectation, array( 'google.com', 'recaptcha.net' ) ) ) ? $expectation : 'google.com';
58
59 return $reality;
60
61 }
62
63
64 /**
65 * Include any necessary files
66 *
67 * @return void
68 */
69 private function include_files() {
70
71 $selection = \WPCF7::get_option( 'iqfix_recaptcha' );
72 $cf7_version = ( defined( 'WPCF7_VERSION' ) ) ? WPCF7_VERSION : \WPCF7::get_option( 'version', '0' );
73
74 // Prevent update from v2 to v3 notice.
75 \WPCF7::update_option( 'recaptcha_v2_v3_warning', false );
76
77 if( empty( $selection ) || version_compare( $cf7_version, '5.1', '<' ) ) {
78 return;
79 }
80
81 include_once plugin_dir_path( __FILE__ ) . 'recaptcha-v2.php';
82
83 if( class_exists( 'Flamingo_Contact' ) ) {
84 include_once plugin_dir_path( __FILE__ ) . 'flamingo.php'; // Flamingo updates
85 }
86
87 }
88
89
90 /**
91 * Save the reCaptcha settings from our options page
92 * @see IQFix_WPCF7_Deity::display_recaptcha_version_subpage()
93 *
94 * @return Boolean
95 */
96 private function save_recaptcha_settings() {
97
98 // Form hasn't POSTed, return early
99 if( ! isset( $_POST, $_POST['iqfix_recaptcha_version'], $_POST['iqfix_wpcf7_submit'] ) ) {
100 return false;
101 }
102
103 // Ensure we have and can verify our nonce. IF not, return early
104 if( ! ( ! empty( $_POST['iqfix_wpcf7_nonce'] ) && wp_verify_nonce( $_POST['iqfix_wpcf7_nonce'], 'iqfix_wpcf7_vers_select' ) ) ) {
105 return false;
106 }
107
108 $selection = intval( $_POST['iqfix_recaptcha_version'] );
109 $source = ( isset( $_POST, $_POST['iqfix_recaptcha_source'] ) ) ? sanitize_text_field( $_POST['iqfix_recaptcha_source'] ) : 'google.com';
110 $source = self::verify_recaptcha_source( $source );
111
112 // Save Network Settings
113 if( is_network_admin() && isset( $_POST['wpcf7_recaptcha_network'] ) ) {
114
115 $sitekey = trim( $_POST['wpcf7_recaptcha_network']['sitekey'] );
116 $secretkey = trim( $_POST['wpcf7_recaptcha_network']['secretkey'] );
117
118 update_site_option( 'network_iqfix_recaptcha', array(
119 'sitekey' => $sitekey,
120 'secret' => $secretkey,
121 'iqfix_recaptcha' => $selection,
122 'recaptcha_source' => $source,
123 ) );
124
125 // Save Regular WPCF7 Settings
126 } else {
127
128 \WPCF7::update_option( 'iqfix_recaptcha', $selection );
129 \WPCF7::update_option( 'iqfix_recaptcha_source', $source );
130
131 }
132
133 return true;
134
135 }
136
137
138 /**
139 * Add any necessary action hooks
140 *
141 * @return void
142 */
143 private function action_hooks() {
144
145 add_action( 'admin_menu', array( $this, 'register_submenus' ) );
146 add_action( 'network_admin_menu', array( $this, 'register_network_submenus' ) );
147
148 }
149
150
151 /**
152 * Add any necessary filter hooks
153 *
154 * @return void
155 */
156 private function filter_hooks() {
157
158 add_filter( 'option_wpcf7', array( $this, 'network_wpcf7_options' ), 9 );
159
160 }
161
162
163 /**
164 * Register submenus for picking ReCaptcha versions
165 *
166 * @return void
167 */
168 public function register_submenus() {
169
170 $cf7_admin_cap = ( defined( 'WPCF7_ADMIN_READ_WRITE_CAPABILITY' ) ) ? WPCF7_ADMIN_READ_WRITE_CAPABILITY : 'publish_pages';
171
172 add_submenu_page(
173 'wpcf7',
174 esc_html__( 'reCaptcha Version', 'wpcf7-recaptcha' ),
175 esc_html__( 'reCaptcha Version', 'wpcf7-recaptcha' ),
176 $cf7_admin_cap,
177 'recaptcha-version',
178 array( $this, 'display_recaptcha_version_subpage' )
179 );
180
181 }
182
183
184 /**
185 * Register submenus for the Network Admin Panel
186 *
187 * @return void
188 */
189 public function register_network_submenus() {
190
191 add_submenu_page(
192 'plugins.php',
193 esc_html__( 'WPCF7 reCaptcha Settings', 'wpcf7-recaptcha' ),
194 esc_html__( 'WPCF7 reCaptcha Settings', 'wpcf7-recaptcha' ),
195 'manage_network_plugins',
196 'recaptcha-version',
197 array( $this, 'display_recaptcha_version_subpage' )
198 );
199
200 }
201
202
203 /**
204 * Display reCaptcha version subpage
205 *
206 * @return void
207 */
208 public function display_recaptcha_version_subpage() {
209
210 $updated = $this->save_recaptcha_settings();
211 $cf7_version = ( defined( 'WPCF7_VERSION' ) ) ? WPCF7_VERSION : \WPCF7::get_option( 'version', '0' );
212
213 // Grab Network Settings
214 if( is_network_admin() ) {
215
216 $network_options = get_site_option( 'network_iqfix_recaptcha' );
217 $selection = ( ! empty( $network_options['iqfix_recaptcha'] ) ) ? $network_options['iqfix_recaptcha'] : '';
218 $source = ( ! empty( $network_options['recaptcha_source'] ) ) ? $network_options['recaptcha_source'] : '';
219 $sitekey = ( ! empty( $network_options['sitekey'] ) ) ? $network_options['sitekey'] : '';
220 $secretkey = ( ! empty( $network_options['secret'] ) ) ? $network_options['secret'] : '';
221
222 // Grab Site Settings
223 } else {
224
225 $selection = \WPCF7::get_option( 'iqfix_recaptcha' );
226 $source = \WPCF7::get_option( 'iqfix_recaptcha_source' );
227
228 }
229
230 // Show simple message
231 if( version_compare( $cf7_version, '5.1', '<' ) ) {
232 printf(
233 '<div class="wrap"><h1>%1$s</h1><p>%2$s</p></div>',
234 esc_html__( 'ReCaptcha v2 for Contact Form 7', 'wpcf7-recaptcha' ),
235 esc_html__( 'This version of Contact Form 7 already uses reCaptcha version 2, you do not need \'ReCaptcha v2 for Contact Form 7\' installed at this time.', 'wpcf7-recaptcha' )
236 );
237 return;
238 }
239
240 ?>
241
242 <div class="wrap">
243 <style>
244 #iqFacebook {margin-top: 40px;}
245 #iqFacebook a {display: inline-block; margin-bottom: 12px;}
246 #iqFacebook img {display: block; max-width: 100%; height: auto;}
247 </style>
248
249 <?php
250
251 printf( '<h1>%1$s</h1>', esc_html__( 'ReCaptcha v2 for Contact Form 7', 'wpcf7-recaptcha' ) );
252
253 if( $updated ) {
254 printf(
255 '<div class="notice updated"><p>%1$s</p></div>',
256 esc_html__( 'Your reCaptcha settings have been updated.', 'wpcf7-recaptcha' )
257 );
258 }
259
260 /* translators: %s is a shortcode example wrapped in <code> tags. */
261 printf( '<p>%1$s</p>',
262 sprintf( esc_html__( 'Only when "reCaptcha Version 2" is selected will you need to use the %s shortcode tag in your Contact Form 7 forms. ReCaptcha v3 (Default Usage) does not use a recaptcha shortcode.%sSelect the version of reCaptcha you would like to use:', 'wpcf7-recaptcha' ),
263 '<code>[recaptcha]</code>',
264 '<br />'
265 )
266 );
267 ?>
268
269 <form method="post">
270 <?php wp_nonce_field( 'iqfix_wpcf7_vers_select', 'iqfix_wpcf7_nonce' ); ?>
271
272 <label for="iqfix_recaptcha_version"><strong><?php esc_html_e( 'Select reCaptcha Usage', 'wpcf7-recaptcha' ); ?>:</strong></label><br />
273 <select id="iqfix_recaptcha_version" name="iqfix_recaptcha_version">
274 <option value="0"><?php esc_html_e( 'Default Usage', 'wpcf7-recaptcha' ); ?></option>
275 <option value="2" <?php selected( $selection, 2, true ); ?>><?php esc_html_e( 'reCaptcha Version 2', 'wpcf7-recaptcha' ); ?></option>
276 </select>
277
278 <?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.', 'wpcf7-recaptcha' ) ); ?>
279
280 <label for="iqfix_recaptcha_source"><strong><?php esc_html_e( 'Select reCaptcha Source', 'wpcf7-recaptcha' ); ?>:</strong></label><br />
281 <select id="iqfix_recaptcha_source" name="iqfix_recaptcha_source">
282 <option value="google.com">google.com</option>
283 <option value="recaptcha.net" <?php selected( $source, 'recaptcha.net', true ); ?>>recaptcha.net</option>
284 </select>
285
286 <?php if( is_network_admin() ) : ?>
287
288 <hr />
289 <h2><?php esc_html__( 'Network Wide Settings', 'wpcf7-recaptcha' ); ?></h2>
290
291 <p><strong><?php _e( 'Please read all of the below before committing to these changes.', 'wpcf7-recaptcha' ); ?></strong></p>
292 <p><?php _e( 'You may set Network wide API keys below. Please ensure that every network site is on the domain list in the Google API Consolefor this API key. ReCaptcha keys can still be set ( or unset ) on a per site basis if necessary.', 'wpcf7-recaptcha' ); ?></p>
293 <p><?php
294 /* translators: Care for HTML in string used for emphasis. */
295 _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' );
296 ?></p>
297
298 <table class="form-table">
299 <tbody>
300 <tr>
301 <th><?php _e( 'Site Key', 'wpcf7-recaptcha' ); ?></th>
302 <td><input type="text" name="wpcf7_recaptcha_network[sitekey]" class="regular-text" value="<?php echo esc_attr( $sitekey ); ?>" /></td>
303 </tr>
304 <tr>
305 <th><?php _e( 'Secret Key', 'wpcf7-recaptcha' ); ?></th>
306 <td><input type="password" name="wpcf7_recaptcha_network[secretkey]" class="regular-text" value="<?php echo esc_attr( $secretkey ); ?>" /></td>
307 </tr>
308 </tbody>
309 </table>
310
311 <?php endif; ?>
312
313 <?php submit_button( esc_html__( 'Submit', 'wpcf7-recaptcha' ), 'submit', 'iqfix_wpcf7_submit' ); ?>
314 </form>
315
316 <div id="iqFacebook">
317 <?php
318 printf( '<a href="%1$s" target="_blank"><img src="%2$s" width="540" height="410" alt="%3$s" /></a>',
319 esc_url( 'https://www.facebook.com/iqcomputing' ),
320 plugins_url( 'assets/images/facebook-like.png', __FILE__ ),
321 /* translators: Image alternative tag. */
322 esc_attr__( 'Like IQComputing on Facebook mascot', 'wpcf7-recaptcha' )
323 );
324 printf( '<p>%1$s</p>',
325 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' )
326 );
327 ?>
328 </div> <!-- id="iqFacebook" -->
329
330 </div>
331
332 <?php
333
334 }
335
336
337 /**
338 * Filter Hook into WPCF7 get option
339 * Maybe replace it work a network option
340 *
341 * @param Mixed $value
342 * @param Stirng $option_name
343 *
344 * @return Mixed $value
345 */
346 public function network_wpcf7_options( $value ) {
347
348 // If we're not on a multisite setup we can skip this filter hook
349 if( ! is_multisite() || empty( $value ) ) {
350 return $value;
351 }
352
353 // Allow sites to be setup regardless of network specifciations
354 if( is_admin()
355 && isset( $_GET, $_GET['page'], $_GET['service'], $_GET['action'] )
356 && 'wpcf7-integration' == $_GET['page']
357 && 'recaptcha' == $_GET['service']
358 && 'setup' == $_GET['action']
359 ) {
360 return $value;
361 }
362
363 // Grab Network Option
364 $network_wpcf7 = get_site_option( 'network_iqfix_recaptcha' );
365 $network_wpcf7 = array_filter( (array)$network_wpcf7 );
366
367 // Set site keys IF there are no API keys set.
368 if( empty( $value['recaptcha'] ) && ! empty( $network_wpcf7['sitekey'] ) && ! empty( $network_wpcf7['secret'] ) ) {
369 $value['recaptcha'] = array(
370 $network_wpcf7['sitekey'] => $network_wpcf7['secret'],
371 );
372 }
373
374 // Set IQFix reCaptcha
375 if( ! isset( $value['iqfix_recaptcha'] ) && ! empty( $network_wpcf7['iqfix_recaptcha'] ) ) {
376 $value['iqfix_recaptcha'] = $network_wpcf7['iqfix_recaptcha'];
377 }
378
379 // Set IQFix reCaptcha Source
380 if( ! isset( $value['iqfix_recaptcha_source'] ) && ! empty( $network_wpcf7['recaptcha_source'] ) ) {
381 $value['iqfix_recaptcha_source'] = $network_wpcf7['recaptcha_source'];
382 }
383
384 return $value;
385
386 }
387
388
389 } // END Class IQFix_WPCF7_Deity
390
391
392 /**
393 * Initialize Class
394 *
395 * @return void
396 */
397 function iqfix_wpcf7_deity_init() {
398
399 if( class_exists( 'WPCF7' ) ) {
400 IQFix_WPCF7_Deity::register();
401 }
402
403 }
404 add_action( 'plugins_loaded', 'iqfix_wpcf7_deity_init' );
405
406
407 /**
408 * Remove upgrade notice from v2 to v3
409 * Prevent api keys from being reset.
410 *
411 * @return void
412 */
413 function iqfix_wpcf7_upgrade_recaptcha_v2_v3_removal() {
414
415 remove_action( 'wpcf7_upgrade', 'wpcf7_upgrade_recaptcha_v2_v3', 10 );
416
417 }
418 add_action( 'admin_init', 'iqfix_wpcf7_upgrade_recaptcha_v2_v3_removal', 9 );