Plugin Directory

source: contact-form-7/trunk/modules/recaptcha/recaptcha.php

Last change on this file was 3043884, checked in by takayukister, 5 months ago

Merge changes on GitHub https://github.com/takayukister/contact-form-7

File size: 5.4 KB
Line 
1<?php
2/**
3 * reCAPTCHA module main file
4 *
5 * @link https://contactform7.com/recaptcha/
6 */
7
8wpcf7_include_module_file( 'recaptcha/service.php' );
9
10
11add_action( 'wpcf7_init', 'wpcf7_recaptcha_register_service', 40, 0 );
12
13/**
14 * Registers the reCAPTCHA service.
15 */
16function wpcf7_recaptcha_register_service() {
17        $integration = WPCF7_Integration::get_instance();
18
19        $integration->add_service( 'recaptcha',
20                WPCF7_RECAPTCHA::get_instance()
21        );
22}
23
24
25add_action(
26        'wp_enqueue_scripts',
27        'wpcf7_recaptcha_enqueue_scripts',
28        20, 0
29);
30
31/**
32 * Enqueues frontend scripts for reCAPTCHA.
33 */
34function wpcf7_recaptcha_enqueue_scripts() {
35        $service = WPCF7_RECAPTCHA::get_instance();
36
37        if ( ! $service->is_active() ) {
38                return;
39        }
40
41        $url = 'https://www.google.com/recaptcha/api.js';
42
43        if ( apply_filters( 'wpcf7_use_recaptcha_net', false ) ) {
44                $url = 'https://www.recaptcha.net/recaptcha/api.js';
45        }
46
47        wp_register_script( 'google-recaptcha',
48                add_query_arg(
49                        array(
50                                'render' => $service->get_sitekey(),
51                        ),
52                        $url
53                ),
54                array(),
55                '3.0',
56                array( 'in_footer' => true )
57        );
58
59        $assets = array();
60        $asset_file = wpcf7_plugin_path( 'modules/recaptcha/index.asset.php' );
61
62        if ( file_exists( $asset_file ) ) {
63                $assets = include( $asset_file );
64        }
65
66        $assets = wp_parse_args( $assets, array(
67                'dependencies' => array(),
68                'version' => WPCF7_VERSION,
69        ) );
70
71        wp_register_script(
72                'wpcf7-recaptcha',
73                wpcf7_plugin_url( 'modules/recaptcha/index.js' ),
74                array_merge(
75                        $assets['dependencies'],
76                        array(
77                                'google-recaptcha',
78                                'wp-polyfill',
79                        )
80                ),
81                $assets['version'],
82                array( 'in_footer' => true )
83        );
84
85        wp_enqueue_script( 'wpcf7-recaptcha' );
86
87        wp_localize_script( 'wpcf7-recaptcha',
88                'wpcf7_recaptcha',
89                array(
90                        'sitekey' => $service->get_sitekey(),
91                        'actions' => apply_filters( 'wpcf7_recaptcha_actions', array(
92                                'homepage' => 'homepage',
93                                'contactform' => 'contactform',
94                        ) ),
95                )
96        );
97}
98
99
100add_filter(
101        'wpcf7_form_hidden_fields',
102        'wpcf7_recaptcha_add_hidden_fields',
103        100, 1
104);
105
106/**
107 * Adds hidden form field for reCAPTCHA.
108 */
109function wpcf7_recaptcha_add_hidden_fields( $fields ) {
110        $service = WPCF7_RECAPTCHA::get_instance();
111
112        if ( ! $service->is_active() ) {
113                return $fields;
114        }
115
116        return array_merge( $fields, array(
117                '_wpcf7_recaptcha_response' => '',
118        ) );
119}
120
121
122add_filter( 'wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9, 2 );
123
124/**
125 * Verifies reCAPTCHA token on the server side.
126 */
127function wpcf7_recaptcha_verify_response( $spam, $submission ) {
128        if ( $spam ) {
129                return $spam;
130        }
131
132        $service = WPCF7_RECAPTCHA::get_instance();
133
134        if ( ! $service->is_active() ) {
135                return $spam;
136        }
137
138        $token = trim( $_POST['_wpcf7_recaptcha_response'] ?? '' );
139
140        if ( $service->verify( $token ) ) { // Human
141                $spam = false;
142        } else { // Bot
143                $spam = true;
144
145                if ( '' === $token ) {
146                        $submission->add_spam_log( array(
147                                'agent' => 'recaptcha',
148                                'reason' => __(
149                                        'reCAPTCHA response token is empty.',
150                                        'contact-form-7'
151                                ),
152                        ) );
153                } else {
154                        $submission->add_spam_log( array(
155                                'agent' => 'recaptcha',
156                                'reason' => sprintf(
157                                        __(
158                                                'reCAPTCHA score (%1$.2f) is lower than the threshold (%2$.2f).',
159                                                'contact-form-7'
160                                        ),
161                                        $service->get_last_score(),
162                                        $service->get_threshold()
163                                ),
164                        ) );
165                }
166        }
167
168        return $spam;
169}
170
171
172add_action( 'wpcf7_init', 'wpcf7_recaptcha_add_form_tag_recaptcha', 10, 0 );
173
174/**
175 * Registers form-tag types for reCAPTCHA.
176 */
177function wpcf7_recaptcha_add_form_tag_recaptcha() {
178        $service = WPCF7_RECAPTCHA::get_instance();
179
180        if ( ! $service->is_active() ) {
181                return;
182        }
183
184        wpcf7_add_form_tag( 'recaptcha',
185                '__return_empty_string', // no output
186                array( 'display-block' => true )
187        );
188}
189
190
191add_action( 'wpcf7_upgrade', 'wpcf7_upgrade_recaptcha_v2_v3', 10, 2 );
192
193/**
194 * Adds warnings for users upgrading from reCAPTCHA v2 to v3.
195 */
196function wpcf7_upgrade_recaptcha_v2_v3( $new_ver, $old_ver ) {
197        if ( version_compare( '5.1-dev', $old_ver, '<=' ) ) {
198                return;
199        }
200
201        $service = WPCF7_RECAPTCHA::get_instance();
202
203        if ( ! $service->is_active() or $service->get_global_sitekey() ) {
204                return;
205        }
206
207        // Maybe v2 keys are used now. Warning necessary.
208        WPCF7::update_option( 'recaptcha_v2_v3_warning', true );
209        WPCF7::update_option( 'recaptcha', null );
210}
211
212
213add_action( 'wpcf7_admin_menu', 'wpcf7_admin_init_recaptcha_v2_v3', 10, 0 );
214
215/**
216 * Adds filters and actions for warnings.
217 */
218function wpcf7_admin_init_recaptcha_v2_v3() {
219        if ( ! WPCF7::get_option( 'recaptcha_v2_v3_warning' ) ) {
220                return;
221        }
222
223        add_filter(
224                'wpcf7_admin_menu_change_notice',
225                'wpcf7_admin_menu_change_notice_recaptcha_v2_v3',
226                10, 1
227        );
228
229        add_action(
230                'wpcf7_admin_warnings',
231                'wpcf7_admin_warnings_recaptcha_v2_v3',
232                5, 3
233        );
234}
235
236
237/**
238 * Increments the admin menu counter for the Integration page.
239 */
240function wpcf7_admin_menu_change_notice_recaptcha_v2_v3( $counts ) {
241        $counts['wpcf7-integration'] += 1;
242        return $counts;
243}
244
245
246/**
247 * Prints warnings on the admin screen.
248 */
249function wpcf7_admin_warnings_recaptcha_v2_v3( $page, $action, $object ) {
250        if ( 'wpcf7-integration' !== $page ) {
251                return;
252        }
253
254        $message = sprintf(
255                esc_html( __(
256                        "API keys for reCAPTCHA v3 are different from those for v2; keys for v2 do not work with the v3 API. You need to register your sites again to get new keys for v3. For details, see %s.",
257                        'contact-form-7'
258                ) ),
259                wpcf7_link(
260                        __( 'https://contactform7.com/recaptcha/', 'contact-form-7' ),
261                        __( 'reCAPTCHA (v3)', 'contact-form-7' )
262                )
263        );
264
265        echo sprintf(
266                '<div class="notice notice-warning"><p>%s</p></div>',
267                $message
268        );
269}
Note: See TracBrowser for help on using the repository browser.