Skip to content

Commit e7fdb2c

Browse files
committed
Update the helper and template.
- Better defualts and config merging - Config overriding when calling display() - `callback` option added - Cleaned up uneeded JS code - Script block is now used by default
1 parent 5dc5906 commit e7fdb2c

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ $this->loadComponent('Recaptcha.Recaptcha', [
3434
'secret' => 'your_secret',
3535
'type' => 'image', // image/audio
3636
'theme' => 'light', // light/dark
37-
'lang' => 'vi', // default en
37+
'lang' => 'en',
3838
'size' => 'normal' // normal/compact
39+
'callback' => null, // `callback` data attribute for the recaptcha div, default `null`
40+
'scriptBlock' => true // Value for `block` option for HtmlHelper::script() call
3941
]);
4042
```
4143

42-
Override default configure from app config file:
44+
Override default config from app config file:
4345

4446
```php
4547
// file: config/app.php
4648

4749
/**
4850
* Recaptcha configuration.
49-
*
5051
*/
5152
'Recaptcha' => [
52-
'enable' => true,
5353
'sitekey' => 'your_site_key',
5454
'secret' => 'your_secret',
5555
'type' => 'image',
@@ -62,7 +62,7 @@ Override default configure from app config file:
6262
Override default configure from recaptcha config file:
6363

6464
```php
65-
// ffile: config/recaptcha.php
65+
// file: config/recaptcha.php
6666

6767
return [
6868
/**
@@ -96,12 +96,12 @@ Config preference:
9696

9797
## Usage
9898

99-
Display recaptcha in your view:
99+
Display recaptcha in your template:
100100

101101
```php
102102
<?= $this->Form->create() ?>
103103
<?= $this->Form->control('email') ?>
104-
// Display recaptcha box in your view, if configure has enable = false, nothing will be displayed
104+
// Display recaptcha box in your template, if configure has enable = false, nothing will be displayed
105105
<?= $this->Recaptcha->display() ?>
106106
<?= $this->Form->button() ?>
107107
<?= $this->Form->end() ?>

src/Controller/Component/RecaptchaComponent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ class RecaptchaComponent extends Component
2929
'secret' => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
3030
'theme' => 'light',
3131
'type' => 'image',
32+
'callback' => null,
3233
'enable' => true,
3334
'lang' => 'en',
3435
'size' => 'normal',
3536
'httpClientOptions' => [],
37+
'scriptBlock' => true,
3638
];
3739

3840
/**

src/View/Helper/RecaptchaHelper.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@
1111
*/
1212
class RecaptchaHelper extends Helper
1313
{
14+
/**
15+
* Default config
16+
*
17+
* These are merged with user-provided config when the component is used.
18+
*
19+
* @var array<string, mixed>
20+
*/
21+
protected array $_defaultConfig = [
22+
// This is test only key/secret
23+
'sitekey' => '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
24+
'secret' => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
25+
'theme' => 'light',
26+
'type' => 'image',
27+
'callback' => null,
28+
'enable' => true,
29+
'lang' => 'en',
30+
'size' => 'normal',
31+
'scriptBlock' => true,
32+
];
33+
34+
/**
35+
* Helpers
36+
*
37+
* @var array
38+
*/
39+
protected array $helpers = ['Form'];
40+
1441
/**
1542
* initialize
1643
*
@@ -26,15 +53,18 @@ public function initialize(array $config = []): void
2653
/**
2754
* Display recaptcha function
2855
*
56+
* @param array $config Config
2957
* @return string
3058
*/
31-
public function display(): string
59+
public function display(array $config = []): string
3260
{
3361
$recaptcha = $this->getConfig();
3462
if (empty($recaptcha['enable'])) {
3563
return '';
3664
}
3765

66+
$recaptcha = $config + $recaptcha;
67+
3868
return $this->_View->element('Recaptcha.recaptcha', compact('recaptcha'));
3969
}
4070
}

templates/element/recaptcha.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
<?= $this->Html->script('https://www.google.com/recaptcha/api.js?hl=' . $recaptcha['lang'] . '&onload=CaptchaCallback&render=explicit') ?>
2-
<script type="text/javascript">
3-
var CaptchaCallback = function() {
4-
var el = document.getElementsByClassName('g-recaptcha');
5-
for (var i = 0; i < el.length; i++)
6-
grecaptcha.render(el[i], {'sitekey' : '<?= $recaptcha['sitekey'] ?>'});
7-
};
8-
</script>
1+
<?php
2+
/**
3+
* @var \App\View\AppView $this
4+
*/
95

6+
try {
7+
$this->Form->unlockField('g-recaptcha-response');
8+
} catch (CakeException) {
9+
// If FormProtectorComponent is not loaded, FormHelper::unlockField() throws an exception in older CakePHP versions.
10+
}
11+
?>
12+
<?= $this->Html->script(
13+
'https://www.google.com/recaptcha/api.js?hl=' . $recaptcha['lang'],
14+
[
15+
'block' => $recaptcha['scriptBlock'],
16+
'async' => true,
17+
'defer' => true,
18+
]
19+
) ?>
1020
<div
1121
class="g-recaptcha"
1222
data-sitekey="<?= $recaptcha['sitekey'] ?>"
1323
data-theme="<?= $recaptcha['theme'] ?>"
1424
data-type="<?= $recaptcha['type'] ?>"
1525
data-size="<?= $recaptcha['size'] ?>"
16-
async defer>
26+
<?php if ($recaptcha['callback']) : ?>
27+
data-callback="<?= $recaptcha['callback'] ?>"
28+
<?php endif ?>
29+
>
1730
</div>
1831
<noscript>
1932
<div>

tests/TestCase/View/Helper/RecaptchaHelperTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,25 @@ public function setUp(): void
2525
$this->Recaptcha = new RecaptchaHelper(
2626
$this->View,
2727
[
28-
'enable' => true,
2928
'sitekey' => 'sitekey',
3029
'theme' => 'theme',
3130
'type' => 'type',
3231
'lang' => 'lang',
3332
'size' => 'size',
33+
'callback' => 'callback',
3434
]
3535
);
3636
}
3737

3838
public function testDisplay(): void
3939
{
4040
$result = $this->Recaptcha->display();
41-
$this->assertTrue(is_string($result));
4241
$this->assertStringContainsString('class="g-recaptcha"', $result);
42+
$this->assertStringContainsString('data-sitekey="sitekey"', $result);
43+
$this->assertStringContainsString('data-theme="theme"', $result);
44+
$this->assertStringContainsString('data-type="type"', $result);
45+
$this->assertStringContainsString('data-size="size"', $result);
46+
$this->assertStringContainsString('data-callback="callback"', $result);
4347

4448
$this->Recaptcha->setConfig('enable', false);
4549
$this->assertEmpty($this->Recaptcha->display());

0 commit comments

Comments
 (0)