PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.1
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / admin / settings / miscellaneous-settings / class-wpsc-ms-recaptcha.php

class-wpsc-ms-recaptcha.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.1, at includes/admin/settings/miscellaneous-settings/class-wpsc-ms-recaptcha.php

295 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_MS_Recaptcha' ) ) :
7
8 final class WPSC_MS_Recaptcha {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // User interface.
18 add_action( 'wp_ajax_wpsc_get_ms_recaptcha', array( __CLASS__, 'load_settings_ui' ) );
19 add_action( 'wp_ajax_wpsc_set_ms_recaptcha', array( __CLASS__, 'save_settings' ) );
20 add_action( 'wp_ajax_wpsc_reset_ms_recaptcha', array( __CLASS__, 'reset_settings' ) );
21
22 // Print in create ticket form and my profile.
23 add_action( 'wpsc_print_tff', array( __CLASS__, 'print_tff' ), 9 );
24 add_action( 'wpsc_my_profile', array( __CLASS__, 'print_tff' ), 9 );
25
26 // TFF! validation.
27 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
28 }
29
30 /**
31 * Reset settings
32 *
33 * @return void
34 */
35 public static function reset() {
36
37 $recaptcha = apply_filters(
38 'wpsc_recaptcha_settings',
39 array(
40 'allow-recaptcha' => 0,
41 'recaptcha-version' => 3,
42 'recaptcha-site-key' => '',
43 'recaptcha-secret-key' => '',
44 )
45 );
46 update_option( 'wpsc-recaptcha-settings', $recaptcha );
47 }
48
49 /**
50 * Settings user interface
51 *
52 * @return void
53 */
54 public static function load_settings_ui() {
55
56 if ( ! WPSC_Functions::is_site_admin() ) {
57 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
58 }
59 $settings = get_option( 'wpsc-recaptcha-settings', array() );?>
60
61 <form action="#" onsubmit="return false;" class="wpsc-frm-ms-recaptcha">
62 <div class="wpsc-dock-container">
63 <?php
64 printf(
65 /* translators: Click here to see the documentation */
66 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
67 '<a href="https://supportcandy.net/docs/google-recaptcha/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
68 );
69 ?>
70 </div>
71 <div class="wpsc-input-group">
72 <div class="label-container">
73 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
74 </div>
75 <select id="wpsc-allow-recaptcha" name="allow-recaptcha">
76 <option <?php selected( $settings['allow-recaptcha'], 1 ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
77 <option <?php selected( $settings['allow-recaptcha'], 0 ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
78 </select>
79 </div>
80 <div class="wpsc-input-group">
81 <div class="label-container">
82 <label for=""><?php esc_attr_e( 'reCaptcha version', 'supportcandy' ); ?></label>
83 </div>
84 <select id="wpsc-recaptcha-version" name="recaptcha-version">
85 <option <?php selected( $settings['recaptcha-version'], '2' ); ?> value="2">2</option>
86 <option <?php selected( $settings['recaptcha-version'], '3' ); ?> value="3">3</option>
87 </select>
88 </div>
89 <div class="wpsc-input-group">
90 <div class="label-container">
91 <label for=""><?php esc_attr_e( 'Site key', 'supportcandy' ); ?></label>
92 </div>
93 <input type="text" id="wpsc-recaptcha-site-key" name="recaptcha-site-key" value="<?php echo esc_attr( $settings['recaptcha-site-key'] ); ?>" autocomplete="off">
94 </div>
95 <div class="wpsc-input-group">
96 <div class="label-container">
97 <label for=""><?php esc_attr_e( 'Secret key', 'supportcandy' ); ?></label>
98 </div>
99 <input type="text" id="wpsc-recaptcha-secret-key" name="recaptcha-secret-key" value="<?php echo esc_attr( $settings['recaptcha-secret-key'] ); ?>" autocomplete="off">
100 </div>
101 <?php do_action( 'wpsc_ms_recaptcha' ); ?>
102 <input type="hidden" name="action" value="wpsc_set_ms_recaptcha">
103 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_ms_recaptcha' ) ); ?>">
104 </form>
105 <div class="setting-footer-actions">
106 <button
107 class="wpsc-button normal primary margin-right"
108 onclick="wpsc_set_ms_recaptcha(this);">
109 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button>
110 <button
111 class="wpsc-button normal secondary"
112 onclick="wpsc_reset_ms_recaptcha(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_reset_ms_recaptcha' ) ); ?>');">
113 <?php esc_attr_e( 'Reset default', 'supportcandy' ); ?></button>
114 </div>
115 <?php
116 wp_die();
117 }
118
119 /**
120 * Save settings
121 *
122 * @return void
123 */
124 public static function save_settings() {
125
126 if ( check_ajax_referer( 'wpsc_set_ms_recaptcha', '_ajax_nonce', false ) != 1 ) {
127 wp_send_json_error( 'Unauthorised request!', 401 );
128 }
129
130 if ( ! WPSC_Functions::is_site_admin() ) {
131 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
132 }
133
134 $recaptcha = apply_filters(
135 'wpsc_set_recaptcha',
136 array(
137 'allow-recaptcha' => isset( $_POST['allow-recaptcha'] ) ? intval( $_POST['allow-recaptcha'] ) : '0',
138 'recaptcha-version' => isset( $_POST['recaptcha-version'] ) ? intval( $_POST['recaptcha-version'] ) : '3',
139 'recaptcha-site-key' => isset( $_POST['recaptcha-site-key'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha-site-key'] ) ) : '',
140 'recaptcha-secret-key' => isset( $_POST['recaptcha-secret-key'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha-secret-key'] ) ) : '',
141 )
142 );
143 update_option( 'wpsc-recaptcha-settings', $recaptcha );
144 wp_die();
145 }
146
147 /**
148 * Reset settings to default
149 *
150 * @return void
151 */
152 public static function reset_settings() {
153
154 if ( check_ajax_referer( 'wpsc_reset_ms_recaptcha', '_ajax_nonce', false ) != 1 ) {
155 wp_send_json_error( 'Unauthorised request!', 401 );
156 }
157
158 if ( ! WPSC_Functions::is_site_admin() ) {
159 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
160 }
161 self::reset();
162 wp_die();
163 }
164
165 /**
166 * Print ticket form field
167 *
168 * @return void
169 */
170 public static function print_tff() {
171
172 $recaptcha = get_option( 'wpsc-recaptcha-settings' );
173 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
174 ?>
175 <script src="https://www.google.com/recaptcha/api.js" async defer></script> <?php // phpcs:ignore ?>
176 <div class="wpsc-tff recaptcha wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12 required wpsc-visible" data-cft="recaptcha">
177 <div class="g-recaptcha" data-sitekey="<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>"></div>
178 </div>
179 <?php
180 }
181 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
182 ?>
183 <script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>"></script> <?php // phpcs:ignore ?>
184 <?php
185 }
186 }
187
188 /**
189 * Validate this type field in create ticket
190 *
191 * @return void
192 */
193 public static function js_validate_ticket_form() {
194
195 $recaptcha = get_option( 'wpsc-recaptcha-settings' );
196 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) :
197 ?>
198 case 'recaptcha':
199 var recaptcha = jQuery("#g-recaptcha-response").val();
200 if (recaptcha === "") {
201 isValid = false;
202 alert("<?php esc_attr_e( 'Captcha not set!', 'supportcandy' ); ?>");
203 }
204 break;
205 <?php
206 echo PHP_EOL;
207 endif;
208 }
209
210 /**
211 * Check whether captcha is valid or not.
212 * No need of nonce varification as we already done this where it is called from.
213 *
214 * @param string $action - action string.
215 * @return void
216 */
217 public static function validate( $action = '' ) {
218
219 $server_http_host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
220
221 $recaptcha = get_option( 'wpsc-recaptcha-settings' );
222 if ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 2 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
223
224 $recaptcha_token = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : ''; // phpcs:ignore
225 if ( ! $recaptcha_token ) {
226 wp_send_json_error( new WP_Error( '001', 'Recaptcha token not set' ), 400 );
227 }
228
229 $url = 'https://www.google.com/recaptcha/api/siteverify';
230 $response = wp_remote_post(
231 $url,
232 array(
233 'method' => 'POST',
234 'body' => array(
235 'secret' => $recaptcha['recaptcha-secret-key'],
236 'response' => $recaptcha_token,
237 ),
238 )
239 );
240
241 if (
242 is_wp_error( $response ) ||
243 $response['response']['code'] != 200
244 ) {
245 wp_send_json_error( new WP_Error( '002', 'Something went wrong with recaptcha!' ), 400 );
246 }
247
248 $response = json_decode( $response['body'] );
249 if ( ! (
250 $response->success &&
251 $response->hostname == $server_http_host
252 ) ) {
253 wp_send_json_error( new WP_Error( '003', 'Invalid recaptcha!' ), 400 );
254 }
255 } elseif ( $recaptcha['allow-recaptcha'] === 1 && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) {
256
257 $recaptcha_token = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : ''; // phpcs:ignore
258 if ( ! $recaptcha_token ) {
259 wp_send_json_error( new WP_Error( '001', 'Recaptcha token not set' ), 400 );
260 }
261
262 $url = 'https://www.google.com/recaptcha/api/siteverify';
263 $response = wp_remote_post(
264 $url,
265 array(
266 'method' => 'POST',
267 'body' => array(
268 'secret' => $recaptcha['recaptcha-secret-key'],
269 'response' => $recaptcha_token,
270 ),
271 )
272 );
273
274 if (
275 is_wp_error( $response ) ||
276 $response['response']['code'] != 200
277 ) {
278 wp_send_json_error( new WP_Error( '002', 'Something went wrong with recaptcha!' ), 400 );
279 }
280
281 $response = json_decode( $response['body'] );
282 if ( ! (
283 $response->success &&
284 $response->action == $action &&
285 $response->hostname == $server_http_host
286 ) ) {
287 wp_send_json_error( new WP_Error( '003', 'Invalid recaptcha!' ), 400 );
288 }
289 }
290 }
291 }
292 endif;
293
294 WPSC_MS_Recaptcha::init();
295