PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / GoogleRecaptchaV3.php

GoogleRecaptchaV3.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/GoogleRecaptchaV3.php

70 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\Services;
4
5 use Averta\WordPress\Utility\JSON;
6 use Depicter\GuzzleHttp\Exception\GuzzleException;
7 use Depicter\Utility\Sanitize;
8
9 class GoogleRecaptchaV3
10 {
11 public function verify( $token ) {
12
13 $secret = \Depicter::options()->get( 'google_recaptcha_secret_key', '' );
14 if ( empty( $secret ) ) {
15 return false ;
16 }
17
18 $options = [
19 'form_params' => [
20 'secret' => $secret,
21 'response' => $token
22 ]
23 ];
24
25 try{
26 $response = \Depicter::remote()->post( 'https://www.google.com/recaptcha/api/siteverify', $options );
27 if ( $response->getStatusCode() != 200 ) {
28 return [
29 'success' => false,
30 'data' => [],
31 'message' => __( 'Could not contact google to verify the request', 'depicter' )
32 ];
33 }
34 $content = $response->getBody()->getContents();
35 $result = JSON::decode( $content, true );
36
37 $scoreThreshold = \Depicter::options()->get( 'google_recaptcha_score_threshold', 0.6 );
38
39 if ( !empty( $result['score'] ) && ( $result['score'] > Sanitize::float( $scoreThreshold ) ) ) {
40 return [
41 'success' => true,
42 'data' => [
43 'host' => $result['hostname'] ?? ''
44 ],
45 /* translators: recaptcha score */
46 'message' => sprintf( __( 'Recaptcha challenge passed successfully. [%s]', 'depicter' ), $result['score'] )
47 ];
48
49 } else {
50 $failMessage = \Depicter::options()->get( 'google_recaptcha_fail_message','' ) . "[" .( $result['score'] ?? '0' ) . "]";
51
52 return [
53 'success' => false,
54 'data' => [
55 'score' => $result['score'] ?? 0,
56 'host' => $result['hostname'] ?? ''
57 ],
58 'message' => $failMessage
59 ];
60 }
61 } catch( GuzzleException $e ){
62 return [
63 'success' => false,
64 'message' => $e->getMessage()
65 ];
66 }
67
68 }
69 }
70