Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 09dca04

Browse files
committed
$attributes added to htmlFormSnippet method and helper (V2)
1 parent 4991b28 commit 09dca04

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/ReCaptchaBuilderV2.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php
@@ -20,6 +21,15 @@
2021
class ReCaptchaBuilderV2 extends ReCaptchaBuilder
2122
{
2223

24+
protected static $allowed_data_attribute = [
25+
"theme",
26+
"size",
27+
"tabindex",
28+
"callback",
29+
"cexpired-allback",
30+
"error-callback",
31+
];
32+
2333
/**
2434
* ReCaptchaBuilderV2 constructor.
2535
*
@@ -35,13 +45,15 @@ public function __construct(string $api_site_key, string $api_secret_key)
3545
/**
3646
* Write ReCAPTCHA HTML tag in your FORM
3747
* Insert before </form> tag
48+
*
49+
* @param null|array $attributes
3850
* @return string
3951
*/
40-
public function htmlFormSnippet(): string
52+
public function htmlFormSnippet(?array $attributes = []): string
4153
{
4254

4355
$data_attributes = [];
44-
$config_data_attributes = $this->getTagAttributes();
56+
$config_data_attributes = array_merge($this->getTagAttributes(), self::cleanAttributes($attributes));
4557

4658
foreach ($config_data_attributes as $k => $v) {
4759
if ($v) {
@@ -93,4 +105,16 @@ public function getOnLoadCallback(): string
93105
return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>";
94106
}
95107

96-
}
108+
/**
109+
* Compare given attributes with allowed attributes
110+
*
111+
* @param array|null $attributes
112+
* @return array
113+
*/
114+
protected static function cleanAttributes(?array $attributes = []): array
115+
{
116+
return array_filter($attributes, function ($k) {
117+
return in_array($k, self::$allowed_data_attribute);
118+
}, ARRAY_FILTER_USE_KEY);
119+
}
120+
}

src/helpers.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - helpers.php
@@ -76,12 +77,13 @@ function htmlFormButton(?string $button_label = 'Submit', ?array $properties = [
7677
if (!function_exists('htmlFormSnippet')) {
7778

7879
/**
80+
* @param null|array $attributes
7981
* @return string
8082
*/
81-
function htmlFormSnippet(): string
83+
function htmlFormSnippet(?array $attributes = []): string
8284
{
8385

84-
return ReCaptcha::htmlFormSnippet();
86+
return ReCaptcha::htmlFormSnippet($attributes);
8587
}
8688
}
8789

@@ -133,4 +135,3 @@ function recaptchaFieldName(): string
133135
return \Biscolab\ReCaptcha\ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME;
134136
}
135137
}
136-

0 commit comments

Comments
 (0)