Skip to content

Commit

Permalink
replace deprecated functions for CakePHP 3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
cake17 committed Apr 28, 2018
1 parent c02d5de commit 650cb41
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 39 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ Google reCAPTCHA for CakePHP 3
[![License](https://poser.pugx.org/cakephp-fr/recaptcha/license.png)](https://packagist.org/packages/cakephp-fr/recaptcha)
[![Total Downloads](https://poser.pugx.org/cakephp-fr/recaptcha/d/total.png)](https://packagist.org/packages/cakephp-fr/recaptcha)

Be careful version 1.0 is for CakePHP 3.6 minimum.
For a previous CakePHP version please use the 0.4.2 version.

## Plugin's Objective ##

This plugin adds functionalities to use the new reCAPTCHA API version 2.0 in
CakePHP projects.

This plugin is still under development... For now multiple widgets on a single page is not available.
This plugin is still under development... For now, multiple widgets on a single page is not available.

## Requirements ##

Expand All @@ -28,7 +31,7 @@ _[Using [Composer](http://getcomposer.org/)]_
Add the plugin to your project's `composer.json` - something like this:

```bash
composer require cakephp-fr/recaptcha:~0.4
composer require cakephp-fr/recaptcha:~1.0
```

You then need to load the plugin, by running:
Expand Down Expand Up @@ -85,7 +88,7 @@ For example:
```php
public function initialize() {
parent::initialize();
if ($this->request->action === 'contact') {
if ($this->request->getParam('action') === 'contact') {
$this->loadComponent('Recaptcha.Recaptcha');
}
}
Expand All @@ -96,7 +99,7 @@ public function contact() {
if ($this->request->is('post')) {
if ($this->Recaptcha->verify()) {
// Here you can validate your data
if (!empty($this->request->data)) {
if (!empty($this->request->getData())) {
$this->Flash->success(__('We will get back to you soon.'));
return $this->redirect($this->referer());
} else {
Expand All @@ -120,11 +123,11 @@ For example:
```php
<?= $this->Form->create() ?>

<?= $this->Form->input('name', [
<?= $this->Form->control('name', [
'label' => __('Your Name'),
// 'default' => $this->request->query('name'); // in case you add the Prg Component
]) ?>
<?= $this->Form->input('message', [
<?= $this->Form->control('message', [
'type' => 'textarea',
// 'default' => $this->request->query('message'); // in case you add the Prg Component
'label' => __('Your Message')
Expand Down Expand Up @@ -178,7 +181,7 @@ high.

## License ##

Copyright (c) [2014-2016] [cakephp-fr]
Copyright (c) [2014-2018] [cakephp-fr]

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
2 changes: 1 addition & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Validate the Configure Data
$validator = new ConfigValidator();

$errors = $validator->errors(Configure::read('Recaptcha'));
$errors = $validator->setErrors(Configure::read('Recaptcha'));

if (!empty($errors)) {
$errMsg = '';
Expand Down
12 changes: 6 additions & 6 deletions src/Controller/Component/RecaptchaComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class RecaptchaComponent extends Component
public function setController($controller)
{
// Add the helper on the fly
if (!in_array('Recaptcha.Recaptcha', $controller->viewBuilder()->helpers())) {
$controller->viewBuilder()->helpers(['Recaptcha.Recaptcha'], true);
if (!in_array('Recaptcha.Recaptcha', $controller->viewBuilder()->setHelpers())) {
$controller->viewBuilder()->setHelpers(['Recaptcha.Recaptcha'], true);
}
}

Expand All @@ -71,7 +71,7 @@ public function startup(Event $event)

// instantiate Recaptcha object that deals with retrieving data from google recaptcha
$this->recaptcha = new Recaptcha(new RecaptchaResponse(), $secret);
$controller = $event->subject();
$controller = $event->getSubject();

$this->setController($controller);
}
Expand All @@ -84,8 +84,8 @@ public function startup(Event $event)
public function verify()
{
$controller = $this->_registry->getController();
if (isset($controller->request->data["g-recaptcha-response"])) {
$gRecaptchaResponse = $controller->request->data["g-recaptcha-response"];
$gRecaptchaResponse = $controller->request->getData("g-recaptcha-response");
if (!empty($gRecaptchaResponse)) {

$resp = $this->recaptcha->verifyResponse(
new Client(),
Expand All @@ -107,6 +107,6 @@ public function verify()
*/
public function errors()
{
return $this->recaptcha->errors();
return $this->recaptcha->setErrors();
}
}
10 changes: 5 additions & 5 deletions src/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ContactController extends AppController
public function initialize()
{
parent::initialize();
if (in_array($this->request->action, ['index', 'multipleWidgets'])) {
if (in_array($this->request->getParam('action'), ['index', 'multipleWidgets'])) {
$this->loadComponent('Recaptcha.Recaptcha');
}
}
Expand All @@ -43,7 +43,7 @@ public function index()
if ($this->request->is('post')) {
if ($this->Recaptcha->verify()) {
// Here you can validate your data instead
if ($contact->execute($this->request->data)) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success(__('We will get back to you soon.'));
} else {
$this->Flash->error(__('There was a problem submitting your form.'));
Expand All @@ -67,14 +67,14 @@ public function multipleWidgets()
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($this->Recaptcha->verify()) {
if ($contact->execute($this->request->data)) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success(__('We will get back to you soon.'));
} else {
$this->Flash->error(__('There was a problem submitting your form.'));
}
} else {
debug($contact);
debug($this->Recaptcha);
// debug($contact);
// debug($this->Recaptcha);
$this->Flash->error(__('Please check your Recaptcha Box.'));
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ protected function _buildSchema(Schema $schema)
*/
protected function _buildValidator(Validator $validator)
{
return $validator->add('name', 'length', [
'rule' => ['minLength', 2],
'message' => 'A name is required'
])->add('email', 'format', [
'rule' => 'email',
'message' => 'A valid email address is required',
]);
return $validator
->add('name', 'length', [
'rule' => ['minLength', 2],
'message' => 'A name is required'
])
->add('email', 'format', [
'rule' => 'email',
'message' => 'A valid email address is required',
]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Template/Contact/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
?>
<?= $this->Form->create($contact) ?>
<?= $this->Form->input('name') ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('body') ?>
<?= $this->Form->control('name') ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('body') ?>
<?= $this->Recaptcha->display([
// This options override global configs
'theme' => 'dark',
Expand Down
6 changes: 3 additions & 3 deletions src/Template/Contact/multiple_widgets.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
?>
<?= $this->Form->create($contact) ?>
<?= $this->Form->input('name') ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('body') ?>
<?= $this->Form->control('name') ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('body') ?>
<?= $this->Form->button('Submit') ?>
<?= $this->Form->end() ?>

Expand Down
14 changes: 7 additions & 7 deletions src/View/Helper/RecaptchaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public function __construct(View $view, $config = [])
// Merge Options given by user in config/recaptcha
$this->config(Configure::read('Recaptcha'));

$lang = $this->config('lang');
$lang = $this->getConfig('lang');
if (empty($lang)) {
$this->config('lang', I18n::locale());
$this->setConfig('lang', I18n::locale());
}
// Validate the Configure Data
$validator = new RecaptchaValidator();
$errors = $validator->errors($this->config());
$errors = $validator->setErrors($this->getConfig());
if (!empty($errors)) {
throw new \Exception(__d('recaptcha', 'One of your recaptcha config value is incorrect'));
// throw an exception with config error that is raised
Expand All @@ -91,11 +91,11 @@ public function __construct(View $view, $config = [])
public function display(array $options = [])
{
// merge options
$options = array_merge($this->config(), $options);
$options = array_merge($this->getConfig(), $options);

// Validate the Configure Data
$validator = new RecaptchaValidator();
$errors = $validator->errors($options);
$errors = $validator->setErrors($options);
if (!empty($errors)) {
throw new \Exception(__d('recaptcha', 'One of your recaptcha config value is incorrect'));
// throw an exception with config error that is raised
Expand Down Expand Up @@ -133,11 +133,11 @@ public function render()
*/
public function widget(array $options = [])
{
$options = array_merge($this->config(), $options);
$options = array_merge($this->getConfig(), $options);

// Validate the Configure Data
$validator = new RecaptchaValidator();
$errors = $validator->errors($options);
$errors = $validator->setErrors($options);
if (!empty($errors)) {
throw new \Exception(__d('recaptcha', 'One of your recaptcha config value is incorrect in a widget'));
// throw an exception with config error that is raised
Expand Down

0 comments on commit 650cb41

Please sign in to comment.