pollen-solutions / recaptcha
Pollen Solutions - Recaptcha Component - Recaptcha V2/V3 integration layer for PHP forms
v1.0.0
2021-08-19 00:00 UTC
Requires
- php: ^7.4 || ^8.0
- google/recaptcha: ^1.2
- pollen-solutions/field: ^1.0
- pollen-solutions/http: ^1.0
- pollen-solutions/support: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- roave/security-advisories: dev-latest
Suggests
- pollen-solutions/asset: Pollen Solutions - Asset Component - Tools to manage assets in web applications.
- pollen-solutions/container: PPollen Solutions - Container Component - PSR-11 Dependency injection container.
- pollen-solutions/form: Pollen Solutions - Form Component - Generator and processor tools for PHP web application forms.
This package is auto-updated.
Last update: 2024-12-20 02:10:10 UTC
README
Pollen Solutions Recaptcha Component provides a recaptcha V2/V3 integration layer for PHP forms.
Installation
composer require pollen-solutions/recaptcha
Basic Usage with Pollen form component
- Install Pollen form component with composer.
composer require pollen-solutions/form
- Create a form, display it and send a submission.
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; use Pollen\Form\FormManager; use Pollen\Recaptcha\Recaptcha; use Pollen\Recaptcha\Form\RecaptchaFormField; $recaptcha = new Recaptcha(); $recaptcha->config( [ /** * Recaptcha version or type (required). * @var int 2|3 */ 'version' => 3, /** * Recaptcha Site key (required). * @var string|null $sitekey */ 'sitekey' => '===== sitekey =====', /** * Recaptcha secret key (required). * @var string|null $secretkey */ 'secretkey' => '===== secretkey =====', /** * Locale in ISO 15897 format. en_US if null. * @var string|null $locale */ 'locale' => null, ] ); $forms = new FormManager(); try { $forms->registerFormFieldDriver('recaptcha', new RecaptchaFormField($recaptcha)); } catch(Throwable $e) { // If the recaptcha form field is already registered. unset($e); } $form = $forms->buildForm( [ 'fields' => [ 'email' => [ 'type' => 'text', ], 'captcha' => [ 'type' => 'recaptcha', ], ], ] )->get(); if ($response = $form->handle()->proceed()) { (new SapiEmitter())->emit($response->psr()); exit; } echo <<< HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test Recaptcha</title> </head> <body> $form HTML; // A Recaptcha widget was then automatically added by the form render. $jsScripts = $recaptcha->getJsScripts(); echo <<< HTML <!-- Recaptcha Scripts --> <script type="text/javascript">/* <![CDATA[ */$jsScripts/* ]]> */</script> <!-- / Recaptcha Scripts --> </body> </html> HTML;
Asset autoloader
By default an asset autoloader places the JS in the HTML footer of all pages in the web application. You might want to call these scripts manually.
In Wordpress application
use Pollen\Recaptcha\RecaptchaInstance; /** @var RecaptchaInstance $recaptcha */ $recaptcha = $this->config(['asset.autoloader' => true]); add_action('wp_print_footer_scripts', function () use ($recaptcha) { echo "<!-- Recaptcha Scripts -->" . "<script type=\"text/javascript\">/* <![CDATA[ */" . $recaptcha->getJsScripts() . "/* ]]> */</script>" . "<!-- / Recaptcha Scripts -->"; });