diff --git a/Console/Adminhtml/Command/Disable.php b/Console/Adminhtml/Command/Disable.php
new file mode 100644
index 0000000..b64681b
--- /dev/null
+++ b/Console/Adminhtml/Command/Disable.php
@@ -0,0 +1,91 @@
+helperData = $helperData;
+ $this->_configWriter = $configWriter;
+
+ parent::__construct($name);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function configure()
+ {
+ $this->setName('mpgooglerecaptcha:backend:disable')
+ ->setDescription(__('Disable backend captcha'));
+
+ parent::configure();
+ }
+
+ /**
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ *
+ * @return int|void|null
+ */
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ if (!$this->helperData->isCaptchaBackend()) {
+ $output->writeln(__('The captcha is disable for your admin website.'));
+ } else {
+ $path = 'googlerecaptcha/backend/enabled';
+ $this->_configWriter->save($path, '0');
+ $output->writeln(__('The captcha backend has been successfully disabled. Please run the flush cache command again'));
+ }
+ }
+}
diff --git a/Console/Adminhtml/Command/Enable.php b/Console/Adminhtml/Command/Enable.php
new file mode 100644
index 0000000..3c1ba7f
--- /dev/null
+++ b/Console/Adminhtml/Command/Enable.php
@@ -0,0 +1,91 @@
+helperData = $helperData;
+ $this->_configWriter = $configWriter;
+
+ parent::__construct($name);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function configure()
+ {
+ $this->setName('mpgooglerecaptcha:backend:enable')
+ ->setDescription(__('Enable backend captcha'));
+
+ parent::configure();
+ }
+
+ /**
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ *
+ * @return int|void|null
+ */
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ if ($this->helperData->isCaptchaBackend()) {
+ $output->writeln(__('The captcha is enabled for your admin website.'));
+ } else {
+ $path = 'googlerecaptcha/backend/enabled';
+ $this->_configWriter->save($path, '1');
+ $output->writeln(__('The captcha backend has been successfully enabled. Please run the flush cache command again'));
+ }
+ }
+}
diff --git a/Observer/Captcha.php b/Observer/Captcha.php
index e53536a..e20fe82 100644
--- a/Observer/Captcha.php
+++ b/Observer/Captcha.php
@@ -24,6 +24,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Request\Http;
+use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Event\Observer;
@@ -67,6 +68,11 @@ class Captcha implements ObserverInterface
*/
protected $redirect;
+ /**
+ * @var RequestInterface
+ */
+ protected $requestInterface;
+
/**
* Captcha constructor.
*
@@ -76,6 +82,7 @@ class Captcha implements ObserverInterface
* @param ActionFlag $actionFlag
* @param ResponseInterface $responseInterface
* @param RedirectInterface $redirect
+ * @param RequestInterface $requestInterface
*/
public function __construct(
HelperData $helperData,
@@ -83,7 +90,8 @@ public function __construct(
ManagerInterface $messageManager,
ActionFlag $actionFlag,
ResponseInterface $responseInterface,
- RedirectInterface $redirect
+ RedirectInterface $redirect,
+ RequestInterface $requestInterface
) {
$this->_helperData = $helperData;
$this->_request = $request;
@@ -91,22 +99,36 @@ public function __construct(
$this->_actionFlag = $actionFlag;
$this->_responseInterface = $responseInterface;
$this->redirect = $redirect;
+ $this->requestInterface = $requestInterface;
}
/**
* @param Observer $observer
+ *
+ * @return array|void
*/
public function execute(Observer $observer)
{
if ($this->_helperData->isEnabled() && $this->_helperData->isCaptchaFrontend()) {
$checkResponse = 1;
+ $captcha = false;
if ($this->_request->getFullActionName() === 'wishlist_index_add') {
return;
}
foreach ($this->_helperData->getFormPostPaths() as $item) {
if ($item !== '' && strpos($this->_request->getRequestUri(), trim($item, ' ')) !== false) {
$checkResponse = 0;
- if ($this->_request->getParam('g-recaptcha-response') !== null) {
+ $captcha = $this->_request->getParam('g-recaptcha-response');
+ // case ajax login
+ if ($item === 'customer/ajax/login' && $captcha === null && $this->_request->isAjax()) {
+ $formData = HelperData::jsonDecode($this->requestInterface->getContent());
+ if (array_key_exists('g-recaptcha-response', $formData)) {
+ $captcha = $formData['g-recaptcha-response'];
+ } else {
+ return $this->redirectUrlError(__('Missing required parameters recaptcha!'));
+ }
+ }
+ if ($captcha !== null) {
$type = $this->_helperData->getRecaptchaType();
$response = $this->_helperData->verifyResponse($type);
if (isset($response['success']) && !$response['success']) {
@@ -117,7 +139,9 @@ public function execute(Observer $observer)
}
}
}
- if ($checkResponse === 1 && $this->_request->getParam('g-recaptcha-response') !== null) {
+
+ if ($checkResponse === 1 &&
+ ($this->_request->getParam('g-recaptcha-response') !== null || $captcha !== false)) {
$this->redirectUrlError(__('Missing Url in "Form Post Paths" configuration field!'));
}
}
@@ -148,7 +172,6 @@ public function redirectUrlError($message)
$this->messageManager->getMessages(true);
$this->messageManager->addErrorMessage($message);
$this->_actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
-
- return $this->_responseInterface->setRedirect($this->redirect->getRefererUrl());
+ $this->_responseInterface->setRedirect($this->redirect->getRefererUrl());
}
}
diff --git a/composer.json b/composer.json
index 216577e..aad1f34 100644
--- a/composer.json
+++ b/composer.json
@@ -6,14 +6,14 @@
"google/recaptcha": "^1.2"
},
"type": "magento2-module",
- "version": "1.1.2",
+ "version": "1.2.0",
"license": "proprietary",
"authors": [
{
"name": "Mageplaza",
"email": "support@mageplaza.com",
"homepage": "https://www.mageplaza.com",
- "role": "Technical Support"
+ "role": "Technical Support "
}
],
"autoload": {
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index 7aedaa5..c617919 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -58,7 +58,7 @@
Mageplaza\GoogleRecaptcha\Model\System\Config\Source\Language
-
+
@@ -151,4 +151,4 @@
-
\ No newline at end of file
+
diff --git a/etc/di.xml b/etc/di.xml
new file mode 100644
index 0000000..db1dd85
--- /dev/null
+++ b/etc/di.xml
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+ - Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command\Enable
+ - Mageplaza\GoogleRecaptcha\Console\Adminhtml\Command\Disable
+
+
+
+
diff --git a/i18n/en_US.csv b/i18n/en_US.csv
index db9c005..bb69467 100644
--- a/i18n/en_US.csv
+++ b/i18n/en_US.csv
@@ -1,3 +1,9 @@
+"Disable backend captcha","Disable backend captcha"
+"The captcha is disable for your admin website.","The captcha is disable for your admin website."
+"The captcha backend has been successfully disabled. Please run the flush cache command again","The captcha backend has been successfully disabled. Please run the flush cache command again"
+"Enable backend captcha","Enable backend captcha"
+"The captcha is enabled for your admin website.","The captcha is enabled for your admin website."
+"The captcha backend has been successfully enabled. Please run the flush cache command again","The captcha backend has been successfully enabled. Please run the flush cache command again"
"The response parameter is missing.","The response parameter is missing."
"The request is invalid or malformed.","The request is invalid or malformed."
"Admin Login","Admin Login"
@@ -91,6 +97,7 @@ Light,Light
message,message
"Missing required parameters recaptcha!","Missing required parameters recaptcha!"
"Missing Url in ""Form Post Paths"" configuration field!","Missing Url in ""Form Post Paths"" configuration field!"
+"You need select captcha","You need select captcha"
"Google Recaptcha","Google Recaptcha"
General,General
Enable,Enable
diff --git a/view/frontend/web/js/captcha.js b/view/frontend/web/js/captcha.js
index cf2035e..5ab37eb 100644
--- a/view/frontend/web/js/captcha.js
+++ b/view/frontend/web/js/captcha.js
@@ -19,8 +19,9 @@
*/
define([
- 'jquery'
-], function ($) {
+ 'jquery',
+ 'mage/translate'
+], function ($,$t) {
'use strict';
$.widget('mageplaza.captcha', {
@@ -109,7 +110,7 @@ define([
var divError = $('');
var checkBox = $('');
- divError.text('You need select captcha').attr('style', 'display:none;color:red');
+ divError.text($t('You need select captcha')).attr('style', 'display:none;color:red');
divCaptcha.attr('id', 'mp' + '_recaptcha_' + number);
checkBox.attr('name', 'mp' + '_recaptcha_' + number);
@@ -208,12 +209,6 @@ define([
} else {
element.submit(function (event) {
- if (element.attr('id') !== 'mpageverify-form') {
- if (!element.valid()) {
- return;
- }
- }
-
if (!self.stopSubmit) {
$.each(self.captchaForm, function (form, value) {
if (element.find('#' + value).length > 0) {