Skip to content

Commit

Permalink
Merge pull request #68 from mageplaza/2.3-develop
Browse files Browse the repository at this point in the history
2.3 develop
  • Loading branch information
Victor-Mageplaza authored Aug 22, 2021
2 parents 0b8e343 + 9fb83e3 commit f249cb1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Block/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
public function getForms()
{
$useLogin = false;
$ageVerification = false;
$this->_dataFormId = $this->_helperData->getFormsFrontend();

foreach ($this->_dataFormId as $item => $value) {
Expand All @@ -112,10 +113,11 @@ public function getForms()
case Forms::TYPE_PRODUCTREVIEW:
$actionName = $this->actionName[4];
break;
case Forms::TYPE_CHANGEPASSWORD:
case Forms::TYPE_EDITACCOUNT:
$actionName = $this->actionName[5];
break;
default:
$ageVerification = true;
$actionName = '';
}
$this->unsetDataFromId($item, $actionName);
Expand All @@ -127,7 +129,7 @@ public function getForms()
}
}

if ($this->isAgeVerificationEnabled()) {
if ($this->isAgeVerificationEnabled() && $ageVerification) {
$this->_dataFormId[] = Forms::TYPE_AGEVERIFICATION;
}

Expand Down
19 changes: 14 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Exception;
use Magento\Checkout\Helper\Data as CheckoutData;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -51,6 +52,11 @@ class Data extends CoreHelper
*/
protected $_formPaths;

/**
* @var EncryptorInterface
*/
protected $encryptor;

/**
* Data constructor.
*
Expand All @@ -59,16 +65,19 @@ class Data extends CoreHelper
* @param StoreManagerInterface $storeManager
* @param CurlFactory $curlFactory
* @param DefaultFormsPaths $formPaths
* @param EncryptorInterface $encryptor
*/
public function __construct(
Context $context,
ObjectManagerInterface $objectManager,
StoreManagerInterface $storeManager,
CurlFactory $curlFactory,
DefaultFormsPaths $formPaths
DefaultFormsPaths $formPaths,
EncryptorInterface $encryptor
) {
$this->_curlFactory = $curlFactory;
$this->_formPaths = $formPaths;
$this->encryptor = $encryptor;

parent::__construct($context, $objectManager, $storeManager);
}
Expand All @@ -84,7 +93,7 @@ public function __construct(
*/
public function getVisibleKey($storeId = null)
{
return $this->getConfigGeneral('visible/api_key', $storeId);
return $this->encryptor->decrypt($this->getConfigGeneral('visible/api_key', $storeId));
}

/**
Expand All @@ -94,7 +103,7 @@ public function getVisibleKey($storeId = null)
*/
public function getVisibleSecretKey($storeId = null)
{
return $this->getConfigGeneral('visible/api_secret', $storeId);
return $this->encryptor->decrypt($this->getConfigGeneral('visible/api_secret', $storeId));
}

/**
Expand Down Expand Up @@ -226,7 +235,7 @@ public function getFormsFrontend($storeId = null)
*/
public function getInvisibleKey($storeId = null)
{
return $this->getConfigGeneral('invisible/api_key', $storeId);
return $this->encryptor->decrypt($this->getConfigGeneral('invisible/api_key', $storeId));
}

/**
Expand All @@ -236,7 +245,7 @@ public function getInvisibleKey($storeId = null)
*/
public function getInvisibleSecretKey($storeId = null)
{
return $this->getConfigGeneral('invisible/api_secret', $storeId);
return $this->encryptor->decrypt($this->getConfigGeneral('invisible/api_secret', $storeId));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Model/System/Config/Source/Frontend/Forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Forms implements ArrayInterface
const TYPE_CREATE = 'body.customer-account-create #form-validate.form-create-account';
const TYPE_FORGOT = '#form-validate.form.password.forget';
const TYPE_CONTACT = '#contact-form';
const TYPE_CHANGEPASSWORD = '#form-validate.form.form-edit-account';
const TYPE_EDITACCOUNT = '#form-validate.form.form-edit-account';
const TYPE_PRODUCTREVIEW = '#review-form';
const TYPE_AGEVERIFICATION = '#mpageverify-form';
const TYPE_FORMSEXTENDED = [
Expand Down Expand Up @@ -84,7 +84,7 @@ public function getOptionHash()
self::TYPE_CREATE => __('Create User'),
self::TYPE_FORGOT => __('Forgot Password'),
self::TYPE_CONTACT => __('Contact Us'),
self::TYPE_CHANGEPASSWORD => __('Change Password'),
self::TYPE_EDITACCOUNT => __('Edit Account'),
self::TYPE_PRODUCTREVIEW => __('Product Review')
];
if ($this->checkModuleEnable('Mageplaza_AgeVerification')) {
Expand All @@ -104,7 +104,7 @@ public function defaultForms()
self::TYPE_CREATE => 'customer/account/createpost/',
self::TYPE_FORGOT => 'customer/account/forgotpasswordpost/',
self::TYPE_CONTACT => 'contact/index/post/',
self::TYPE_CHANGEPASSWORD => 'customer/account/editPost/',
self::TYPE_EDITACCOUNT => 'customer/account/editPost/',
self::TYPE_PRODUCTREVIEW => 'review/product/post/'
];
if ($this->checkModuleEnable('Mageplaza_AgeVerification')) {
Expand Down
3 changes: 2 additions & 1 deletion Observer/Adminhtml/Forgot.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function execute(Observer $observer)
{
if ($this->_helperData->isCaptchaBackend()
&& $this->_request->getParam('g-recaptcha-response') !== null
&& in_array('backend_forgotpassword', $this->_helperData->getFormsBackend(), true)
&& (in_array('backend_forgotpassword', $this->_helperData->getFormsBackend(), true))
&& ($this->_helperData->getVisibleKey() !== null && $this->_helperData->getVisibleSecretKey() !== null)
) {
$controller = $this->_urlInterface->getCurrentUrl();
try {
Expand Down
3 changes: 2 additions & 1 deletion Observer/Adminhtml/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function __construct(
public function execute(Observer $observer)
{
if ($this->_helperData->isCaptchaBackend()
&& in_array('backend_login', $this->_helperData->getFormsBackend(), true)
&& (in_array('backend_login', $this->_helperData->getFormsBackend(), true))
&& ($this->_helperData->getVisibleKey() !== null && $this->_helperData->getVisibleSecretKey() !== null)
) {
$response = $this->_helperData->verifyResponse('visible');
if (!array_key_exists('success', $response) || empty($response['success'])) {
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,21 @@ A: Yes, you can choose a language code to display reCAPTCHA on the backend.

Run the following command in Magento 2 root folder:

With Marketing Automation (recommend):

```
composer require mageplaza/module-google-recaptcha mageplaza/module-smtp
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
```

Without Marketing Automation:
```
composer require mageplaza/module-google-recaptcha
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
```

## 4. Google reCAPTCHA highlight features

### Invisible reCAPTCHA
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"google/recaptcha": "^1.2"
},
"type": "magento2-module",
"version": "1.2.2",
"version": "1.2.3",
"license": "proprietary",
"authors": [
{
Expand Down
15 changes: 11 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,32 @@
<field id="enabled" translate="label comment" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[This extension is fully compatible with <a href="https://www.mageplaza.com/magento-2-one-step-checkout-extension/" target="_blank">One Step Checkout</a>. <br>
1. Increase 25% conversion rate with <a href="https://www.mageplaza.com/magento-2-layered-navigation-extension/" target="_blank">Layered Navigation</a><br>
2. Magento stores see upwards of 30% revenue 💰 with AVADA. <a href="https://go.avada.io/mageplaza">Learn more</a>]]></comment>
</field>
<group id="invisible" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Invisible</label>
<field id="api_key" translate="label comment" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="api_key" translate="label comment" type="obscure" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<comment><![CDATA[Create app <a href="https://www.google.com/recaptcha/admin" target="_blank">here</a>.]]></comment>
</field>
<field id="api_secret" translate="label comment" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="api_secret" translate="label comment" type="obscure" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google API Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
</group>
<group id="visible" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Visible</label>
<field id="api_key" translate="label comment" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="api_key" translate="label comment" type="obscure" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<comment><![CDATA[Create app <a href="https://www.google.com/recaptcha/admin" target="_blank">here</a>.]]></comment>
</field>
<field id="api_secret" translate="label comment" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="api_secret" translate="label comment" type="obscure" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google API Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
</group>
<field id="language" translate="label comment" sortOrder="3" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
25 changes: 24 additions & 1 deletion view/frontend/web/js/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ define([
&& $(element).prop("tagName").toLowerCase() === 'form') {
self.activeForm.push(element);
result = true;
} else if (element === '.opc-wrapper.one-step-checkout-wrapper') {
self.activeForm.push(element);
result = true;
}
});
}
Expand All @@ -108,7 +111,7 @@ define([
var divCaptcha = $('<div class="g-recaptcha"></div>');
var divAction = $('.actions-toolbar');
var divError = $('<div class="g-recaptcha-error"></div>');
var checkBox = $('<input type="checkbox" style="visibility: hidden" data-validate="{required:true}" class="mage-error">');
var checkBox = $('<input type="checkbox" style="visibility: hidden" data-validate="{required:false}" class="mage-error">');

divError.text($t('You need select captcha')).attr('style', 'display:none;color:red');
divCaptcha.attr('id', 'mp' + '_recaptcha_' + number);
Expand Down Expand Up @@ -143,6 +146,7 @@ define([
|| value === '#social-form-create'
|| value === '#social-form-password-forget'
|| value === '.popup-authentication #login-form.form.form-login'
|| value === '.opc-wrapper.one-step-checkout-wrapper'
|| (value === '#review-form' && self.options.type === 'invisible')
) {
buttonElement.trigger('click');
Expand Down Expand Up @@ -182,8 +186,26 @@ define([
|| value === '#social-form-create'
|| value === '#social-form-password-forget'
|| value === '.popup-authentication #login-form.form.form-login'
|| value === '.opc-wrapper.one-step-checkout-wrapper'
|| (value === '#review-form' && self.options.type === 'invisible')
) {
if (value === '.opc-wrapper.one-step-checkout-wrapper') {
$('button.checkout').on('click', function (event) {
if (!self.stopSubmit) {
$.each(self.captchaForm, function (form, value) {
if (element.find('#' + value).length > 0) {
grecaptcha.reset(form);
grecaptcha.execute(form);
resetForm = form;
event.preventDefault(event);
event.stopImmediatePropagation();

return false;
}
});
}
});
}
buttonElement.on('click', function (event) {
if (!(element.validation() && element.validation('isValid'))) {
return;
Expand Down Expand Up @@ -256,6 +278,7 @@ define([
$.each(self.captchaForm, function (form, value) {
if (element.find('#' + value).length > 0) {
self.showMessage(divError, 5000);
buttonElement.attr('disabled', false);
grecaptcha.reset(form);
event.preventDefault(event);
event.stopImmediatePropagation();
Expand Down

0 comments on commit f249cb1

Please sign in to comment.