Skip to content

Commit 8b7887e

Browse files
committed
updated
1 parent 0b2ec4f commit 8b7887e

File tree

9 files changed

+126
-80
lines changed

9 files changed

+126
-80
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,16 @@ It's recommended to include reCAPTCHA v3 on every page which can help you get th
234234
In your main homepage or layout page, put the following script at the bottom of your page:
235235

236236
``` html
237-
{!! GoogleReCaptchaV3::background() !!}
237+
{!! GoogleReCaptchaV3::init() !!}
238238
```
239239

240-
Note: the above script MUST be called after GoogleReCaptchaV3::render(), otherwise the google script might run multiple times.
241240

242241
You can also set the background reCAPTCHA as hidden or visible(bottomright):
243242

244243
``` php
245244
...
246245
'background_badge_display' => true, // if false, the badge will be invisible.
246+
'background_mode' => false, // if true, the script will run on every page if you put init() on the global page
247247
...
248248
```
249249

@@ -274,6 +274,10 @@ Include GoogleReCaptchaV3::render() script after your form, params should follow
274274
</form>
275275

276276
{!! GoogleReCaptchaV3::render(['contact_us_id'=>'contact_us', 'signup_id'=>'signup']) !!}
277+
278+
// or
279+
280+
{!! GoogleReCaptchaV3::renderOne('contact_us_id','contact_us') !!} // renderOne($id,$action)
277281
```
278282

279283

config/googlerecaptchav3.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@
7777
|
7878
*/
7979
'background_badge_display' => true,
80+
/*
81+
|--------------------------------------------------------------------------
82+
| Background Badge Style
83+
|--------------------------------------------------------------------------
84+
| Type: boolean
85+
| Support:
86+
| - true: the script will run on every page if you put init() on the global page
87+
| - false: the script will only be running if there is action defined
88+
|
89+
*/
90+
'background_mode' => false,
8091

8192
/*
8293
|--------------------------------------------------------------------------
@@ -111,7 +122,7 @@
111122
| Type: array
112123
| Define a list of ip that you want to skip
113124
*/
114-
'skip_ips'=>[
125+
'skip_ips' => [
115126

116127
],
117128
/*

resources/views/googlerecaptchav3/background.blade.php

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
<script>
1+
@if(! $hasAction && $backgroundMode )
2+
@if($display === false)
3+
<style>
4+
.grecaptcha-badge {
5+
display: none;
6+
}
7+
</style>
8+
@endif
9+
<script>
10+
if (!document.getElementById('gReCaptchaScript')) {
11+
let reCaptchaScript = document.createElement('script');
12+
reCaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js?render={{$publicKey}}');
13+
reCaptchaScript.async = true;
14+
reCaptchaScript.defer = true;
15+
document.head.appendChild(reCaptchaScript);
16+
}
17+
</script>
18+
@endif
219

3-
function onloadCallback() {
4-
@foreach($mappers as $action=>$fields)
5-
@foreach($fields as $field)
6-
let client{{$field}} = grecaptcha.render('{{$field}}', {
7-
'sitekey': '{{$publicKey}}',
8-
@if($inline===true) 'badge': 'inline', @endif
9-
'size': 'invisible',
10-
'hl': '{{$language}}'
11-
});
12-
grecaptcha.ready(function () {
13-
grecaptcha.execute(client{{$field}}, {
14-
action: '{{$action}}'
20+
21+
@if($hasAction)
22+
<script>
23+
function onloadCallback() {
24+
@foreach($mappers as $action=>$fields)
25+
@foreach($fields as $field)
26+
let client{{$field}} = grecaptcha.render('{{$field}}', {
27+
'sitekey': '{{$publicKey}}',
28+
@if($inline===true) 'badge': 'inline', @endif
29+
'size': 'invisible',
30+
'hl': '{{$language}}'
31+
});
32+
grecaptcha.ready(function () {
33+
grecaptcha.execute(client{{$field}}, {
34+
action: '{{$action}}'
35+
});
1536
});
16-
});
17-
@endforeach
18-
@endforeach
19-
}
20-
</script>
21-
<script id='gReCaptchaScript' src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback" defer
22-
async></script>
37+
@endforeach
38+
@endforeach
39+
}
40+
</script>
41+
<script id='gReCaptchaScript' src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback"
42+
defer
43+
async></script>
44+
@endif
45+

src/Configurations/ReCaptchaConfigV3.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,18 @@ public function getSkipIps()
103103
}
104104

105105
/**
106-
* @return \Illuminate\Config\Repository|mixed
106+
* @return bool
107107
*/
108108
public function getBackgroundBadgeDisplay()
109109
{
110110
return config('googlerecaptchav3.background_badge_display');
111111
}
112+
113+
/**
114+
* @return bool
115+
*/
116+
public function shouldEnableBackgroundMode()
117+
{
118+
return config('googlerecaptchav3.background_mode');
119+
}
112120
}

src/Facades/GoogleReCaptchaV3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setAction($value)
1111
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setScore($value)
1212
* @method static render($mappers)
13-
* @method static background()
13+
* @method static renderOne($id,$action)
1414
* @see \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3
1515
*/
1616
class GoogleReCaptchaV3 extends Facade

src/GoogleReCaptchaV3.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ class GoogleReCaptchaV3
1616
{
1717
private $service;
1818
private $defaultTemplate = 'GoogleReCaptchaV3::googlerecaptchav3.template';
19-
private $defaultBackgroundTemplate = 'GoogleReCaptchaV3::googlerecaptchav3.background';
19+
2020
public static $hasAction = false;
2121

22+
public static $collection = [];
23+
2224
public function __construct(GoogleReCaptchaV3Service $service)
2325
{
2426
$this->service = $service;
@@ -30,7 +32,6 @@ public function __construct(GoogleReCaptchaV3Service $service)
3032
*/
3133
public function prepareViewData($mappers)
3234
{
33-
self::$hasAction = true;
3435
$prepareData = [];
3536
foreach ($mappers as $id => $action) {
3637
$prepareData[$action][] = $id;
@@ -49,7 +50,19 @@ public function prepareViewData($mappers)
4950
/**
5051
* @return array
5152
*/
52-
public function prepareBackgroundData()
53+
public function prepareData()
54+
{
55+
if (self::$hasAction) {
56+
return $this->prepareViewData(self::$collection);
57+
} else {
58+
return $this->prepareBackgroundViewData();
59+
}
60+
}
61+
62+
/**
63+
* @return array
64+
*/
65+
public function prepareBackgroundViewData()
5366
{
5467
return [
5568
'publicKey' => $this->getConfig()->getSiteKey(),
@@ -60,36 +73,41 @@ public function prepareBackgroundData()
6073
/**
6174
* @return \Illuminate\Contracts\View\View|mixed
6275
*/
63-
public function background()
76+
public function init()
6477
{
65-
if (self::$hasAction) {
78+
if (!$this->getConfig()->isServiceEnabled()) {
6679
return;
6780
}
81+
$default = [
82+
'hasAction' => self::$hasAction,
83+
'backgroundMode' => $this->getConfig()->shouldEnableBackgroundMode()
84+
];
6885

69-
return app('view')->make($this->getBackgroundView(), $this->prepareBackgroundData());
86+
return app('view')->make($this->getView(), array_merge($this->prepareData(), $default));
7087
}
7188

7289
/**
73-
* @param $mappers
74-
* @return \Illuminate\Contracts\View\View|null
90+
* @param $id
91+
* @param $action
7592
*/
76-
public function render($mappers = [])
93+
public function renderOne($id, $action)
7794
{
78-
if (! $this->getConfig()->isServiceEnabled()) {
79-
return;
80-
}
81-
82-
return app('view')->make($this->getView(), $this->prepareViewData($mappers));
95+
self::$hasAction = true;
96+
self::$collection[$id] = $action;
8397
}
8498

8599
/**
86-
* @return mixed|string
100+
* @param $mappers
87101
*/
88-
protected function getBackgroundView()
102+
public function render($mappers)
89103
{
90-
return $this->defaultBackgroundTemplate;
104+
self::$hasAction = true;
105+
foreach ($mappers as $id => $action) {
106+
self::$collection[$id] = $action;
107+
}
91108
}
92109

110+
93111
/**
94112
* @return mixed|string
95113
*/

src/Interfaces/ReCaptchaConfigV3Interface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ public function getSkipIps();
7474
* @return bool
7575
*/
7676
public function getBackgroundBadgeDisplay();
77+
78+
79+
/**
80+
* @return bool
81+
*/
82+
public function shouldEnableBackgroundMode();
7783
}

tests/ViewTest.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,32 @@ public function testView()
2424
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
2525
$service = new GoogleReCaptchaV3($_service);
2626

27-
$data = $service->render(['contact_us_id' => 'contact_us']);
27+
$service->renderOne('contact_us_id', 'contact_us');
28+
$data = $service->init();
2829
$this->assertEquals(null, $data);
2930
}
3031

31-
public function testView2()
32+
public function testView1()
3233
{
3334
// Create a stub for the SomeClass class.
3435
$configStub = $this->createMock(ReCaptchaConfigV3::class);
3536

3637
// Configure the stub.
3738
$configStub->method('isServiceEnabled')
38-
->willReturn(true);
39-
40-
$configStub->method('getSiteKey')
41-
->willReturn('test1');
42-
43-
$configStub->method('isInline')
4439
->willReturn(false);
4540

46-
$configStub->method('getLanguage')
47-
->willReturn('en');
48-
4941
$clientStub = $this->createMock(GuzzleRequestClient::class);
5042

5143
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
5244
$service = new GoogleReCaptchaV3($_service);
5345

54-
$data = $service->prepareViewData(['contact_us_id' => 'contact_us']);
55-
$this->assertEquals('test1', $data['publicKey']);
56-
$this->assertEquals('contact_us_id', $data['mappers']['contact_us'][0]);
57-
$this->assertEquals(false, $data['inline']);
58-
$this->assertEquals('en', $data['language']);
46+
$service->renderOne('contact_us_id', 'contact_us');
47+
$service->render(['contact_us_id1'=> 'contact_us']);
48+
$this->assertEquals(null, $service::$collection);
5949
}
6050

61-
public function testView3()
51+
52+
public function testView2()
6253
{
6354
// Create a stub for the SomeClass class.
6455
$configStub = $this->createMock(ReCaptchaConfigV3::class);
@@ -81,9 +72,11 @@ public function testView3()
8172
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
8273
$service = new GoogleReCaptchaV3($_service);
8374

84-
$service->prepareViewData(['contact_us_id' => 'contact_us']);
85-
$background = $service->background();
86-
$this->assertEquals('', $background);
75+
$data = $service->prepareViewData(['contact_us_id' => 'contact_us']);
76+
$this->assertEquals('test1', $data['publicKey']);
77+
$this->assertEquals('contact_us_id', $data['mappers']['contact_us'][0]);
78+
$this->assertEquals(false, $data['inline']);
79+
$this->assertEquals('en', $data['language']);
8780
}
8881

8982
public function testView4()
@@ -111,7 +104,7 @@ public function testView4()
111104

112105
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
113106
$service = new GoogleReCaptchaV3($_service);
114-
$data = $service->prepareBackgroundData();
107+
$data = $service->prepareData();
115108
$this->assertEquals(false, $data['display']);
116109
$this->assertEquals('test1', $data['publicKey']);
117110
}

0 commit comments

Comments
 (0)