Commit 1bafc19a authored by Nguyen Van Anh's avatar Nguyen Van Anh Committed by GitHub

Merge pull request #86 from dosarkz/master

Explicit rendering after an onload callback
parents bc936d1a 30599897
...@@ -32,6 +32,12 @@ NOCAPTCHA_SITEKEY=site-key ...@@ -32,6 +32,12 @@ NOCAPTCHA_SITEKEY=site-key
### Usage ### Usage
##### Init js source
```
{!! app('captcha')->renderJs($lang = 'en', $callback = false, $onLoadClass = 'recaptchaCallback') !!}
```
##### Display reCAPTCHA ##### Display reCAPTCHA
```php ```php
...@@ -41,7 +47,7 @@ NOCAPTCHA_SITEKEY=site-key ...@@ -41,7 +47,7 @@ NOCAPTCHA_SITEKEY=site-key
With custom attributes and language support: With custom attributes and language support:
``` ```
{!! app('captcha')->display($attributes = [], $lang = null); !!} {!! app('captcha')->display($attributes = []); !!}
``` ```
##### Validation ##### Validation
......
...@@ -9,6 +9,8 @@ class NoCaptcha ...@@ -9,6 +9,8 @@ class NoCaptcha
{ {
const CLIENT_API = 'https://www.google.com/recaptcha/api.js'; const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
const ON_LOAD_CLASS = 'onloadCallBack';
const RENDER_TYPE = 'explicit';
/** /**
* The recaptcha secret key. * The recaptcha secret key.
...@@ -47,18 +49,26 @@ class NoCaptcha ...@@ -47,18 +49,26 @@ class NoCaptcha
* Render HTML captcha. * Render HTML captcha.
* *
* @param array $attributes * @param array $attributes
* @param string $lang
* *
* @return string * @return string
*/ */
public function display($attributes = [], $lang = null) public function display($attributes = [])
{ {
$attributes['data-sitekey'] = $this->sitekey; $attributes['data-sitekey'] = $this->sitekey;
return '<div class="g-recaptcha"'.$this->buildAttributes($attributes).'></div>';
}
$html = '<script src="'.$this->getJsLink($lang).'" async defer></script>'."\n"; /**
$html .= '<div class="g-recaptcha"'.$this->buildAttributes($attributes).'></div>'; * Render js source
*
return $html; * @param null $lang
* @param bool $callback
* @param string $onLoadClass
* @return string
*/
public function renderJs($lang = null, $callback = false, $onLoadClass = 'onloadCallBack')
{
return '<script src="'.$this->getJsLink($lang, $callback, $onLoadClass).'" async defer></script>'."\n";
} }
/** /**
...@@ -103,12 +113,29 @@ class NoCaptcha ...@@ -103,12 +113,29 @@ class NoCaptcha
* Get recaptcha js link. * Get recaptcha js link.
* *
* @param string $lang * @param string $lang
* * @param boolean $callback
* @param string $onLoadClass
* @return string * @return string
*/ */
public function getJsLink($lang = null) public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloadCallBack')
{
$client_api = static::CLIENT_API;
$params = [];
$callback ? $this->setCallBackParams($params, $onLoadClass) : false;
$lang ? $params['hl'] = $lang : null;
return $client_api . '?'. http_build_query($params);
}
/**
* @param $params
* @param $onLoadClass
*/
protected function setCallBackParams(&$params, $onLoadClass)
{ {
return $lang ? static::CLIENT_API.'?hl='.$lang : static::CLIENT_API; $params['render'] = 'explicit';
$params['onload'] = $onLoadClass;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment