Skip to content

Commit acaa9cd

Browse files
ADD default badge in config
1 parent 47def97 commit acaa9cd

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ return [
7171
|
7272
*/
7373
'score_threshold' => env('GOOGLE_RECAPTCHA_V3_SCORE_THRESHOLD', 0.5),
74+
75+
/*
76+
|--------------------------------------------------------------------------
77+
| Badge
78+
|--------------------------------------------------------------------------
79+
|
80+
| The default badge position for the reCAPTCHA badge.
81+
| Available options: 'bottomright', 'bottomleft', 'inline', 'hidden'.
82+
| Default is 'bottomright'.
83+
|
84+
*/
85+
'badge' => env('GOOGLE_RECAPTCHA_V3_BADGE', 'bottomright'),
7486
];
7587
```
7688

@@ -83,6 +95,7 @@ GOOGLE_RECAPTCHA_V3_ENABLED=true
8395
GOOGLE_RECAPTCHA_V3_SITE_KEY=your-site-key-here
8496
GOOGLE_RECAPTCHA_V3_SECRET_KEY=your-secret-key-here
8597
GOOGLE_RECAPTCHA_V3_SCORE_THRESHOLD=0.5
98+
GOOGLE_RECAPTCHA_V3_BADGE=bottomright
8699
```
87100

88101
You can obtain your site key and secret key from the [Google reCAPTCHA Admin Console](https://www.google.com/recaptcha/admin).
@@ -91,7 +104,13 @@ You can obtain your site key and secret key from the [Google reCAPTCHA Admin Con
91104

92105
### Frontend Integration
93106

94-
Add the reCAPTCHA script to your Blade templates using the `@recaptcha` directive. You can customize the badge position by passing one of the available badge positions:
107+
Add the reCAPTCHA script to your Blade templates using the `@recaptcha` directive:
108+
109+
```blade
110+
@recaptcha
111+
```
112+
113+
You can also customize the badge position by passing one of the available badge positions:
95114

96115
```blade
97116
<!DOCTYPE html>

config/google-recaptcha-v3.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@
4545
|
4646
*/
4747
'score_threshold' => null,
48+
49+
/*
50+
|--------------------------------------------------------------------------
51+
| Badge
52+
|--------------------------------------------------------------------------
53+
|
54+
| The default badge position for the reCAPTCHA badge.
55+
| Available options: 'bottomright', 'bottomleft', 'inline', 'hidden'.
56+
| Default is 'bottomright'.
57+
|
58+
*/
59+
'badge' => null,
4860
];

src/GoogleRecaptchaV3.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class GoogleRecaptchaV3
1313
{
1414
public function boot(): void
1515
{
16-
Blade::directive('recaptcha', fn (string $expression) => (
16+
Blade::directive('recaptcha', fn (string $expression = '') => (
1717
"<?php echo app(\Maize\GoogleRecaptchaV3\GoogleRecaptchaV3::class)->renderHtml({$expression}); ?>"
1818
));
1919

@@ -69,8 +69,10 @@ private function getHiddenBadgeStyle(): string
6969
CSS;
7070
}
7171

72-
public function renderHtml(Badge $badge): string
72+
public function renderHtml(?Badge $badge = null): string
7373
{
74+
$badge ??= Config::getBadge();
75+
7476
if (! Config::isEnabled()) {
7577
return '';
7678
}

src/Support/Config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Maize\GoogleRecaptchaV3\Support;
44

55
use Illuminate\Support\Uri;
6+
use Maize\GoogleRecaptchaV3\Enums\Badge;
67

78
class Config
89
{
@@ -53,4 +54,19 @@ public static function getScoreThreshold(): float
5354

5455
return blank($threshold) ? 0.5 : floatval($threshold);
5556
}
57+
58+
public static function getBadge(): Badge
59+
{
60+
$badge = config('google-recaptcha-v3.badge');
61+
62+
if ($badge instanceof Badge) {
63+
return $badge;
64+
}
65+
66+
if (is_string($badge)) {
67+
return Badge::tryFrom($badge) ?? Badge::BOTTOMRIGHT;
68+
}
69+
70+
return Badge::BOTTOMRIGHT;
71+
}
5672
}

tests/ConfigTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Maize\GoogleRecaptchaV3\Enums\Badge;
34
use Maize\GoogleRecaptchaV3\Support\Config;
45

56
it('returns base js script url with render parameter', function () {
@@ -94,3 +95,20 @@
9495
'null defaults to 0.5' => [null, 0.5],
9596
'empty string defaults to 0.5' => ['', 0.5],
9697
]);
98+
99+
it('returns badge correctly', function (mixed $value, Badge $expected) {
100+
config()->set('google-recaptcha-v3.badge', $value);
101+
102+
expect(Config::getBadge())->toBe($expected);
103+
})->with([
104+
'Badge enum BOTTOMRIGHT' => [Badge::BOTTOMRIGHT, Badge::BOTTOMRIGHT],
105+
'Badge enum BOTTOMLEFT' => [Badge::BOTTOMLEFT, Badge::BOTTOMLEFT],
106+
'Badge enum INLINE' => [Badge::INLINE, Badge::INLINE],
107+
'Badge enum HIDDEN' => [Badge::HIDDEN, Badge::HIDDEN],
108+
'string bottomright' => ['bottomright', Badge::BOTTOMRIGHT],
109+
'string bottomleft' => ['bottomleft', Badge::BOTTOMLEFT],
110+
'string inline' => ['inline', Badge::INLINE],
111+
'string hidden' => ['hidden', Badge::HIDDEN],
112+
'null defaults to BOTTOMRIGHT' => [null, Badge::BOTTOMRIGHT],
113+
'invalid string defaults to BOTTOMRIGHT' => ['invalid', Badge::BOTTOMRIGHT],
114+
]);

0 commit comments

Comments
 (0)