Skip to content

Commit 89c7e9b

Browse files
authored
Merge pull request #251 from magento/1.0.0-master
Merge 1.0.0-master into 1.0-develop
2 parents 08267b0 + 92e6f43 commit 89c7e9b

File tree

12 files changed

+145
-40
lines changed

12 files changed

+145
-40
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* global grecaptcha */
7+
define([
8+
'jquery'
9+
], function ($) {
10+
'use strict';
11+
12+
var reCaptchaEntities = [],
13+
initialized = false,
14+
rendererRecaptchaId = 'recaptcha-invisible',
15+
rendererReCaptcha = null;
16+
17+
return {
18+
/**
19+
* Add reCaptcha entity to checklist.
20+
*
21+
* @param {jQuery} reCaptchaEntity
22+
* @param {Object} parameters
23+
*/
24+
add: function (reCaptchaEntity, parameters) {
25+
if (!initialized) {
26+
this.init();
27+
grecaptcha.render(rendererRecaptchaId, parameters);
28+
setInterval(this.resolveVisibility, 100);
29+
initialized = true;
30+
}
31+
32+
reCaptchaEntities.push(reCaptchaEntity);
33+
},
34+
35+
/**
36+
* Show additional reCaptcha instance if any other should be visible, otherwise hide it.
37+
*/
38+
resolveVisibility: function () {
39+
reCaptchaEntities.some(function (entity) {
40+
return entity.is(':visible') &&
41+
// 900 is some magic z-index value of modal popups.
42+
(entity.closest('[data-role=\'modal\']').length === 0 || entity.zIndex() > 900);
43+
}) ? rendererReCaptcha.show() : rendererReCaptcha.hide();
44+
},
45+
46+
/**
47+
* Initialize additional reCaptcha instance.
48+
*/
49+
init: function () {
50+
rendererReCaptcha = $('<div/>', {
51+
'id': rendererRecaptchaId
52+
});
53+
$('body').append(rendererReCaptcha);
54+
}
55+
};
56+
});

ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
* See COPYING.txt for license details.
44
*/
55

6-
/* eslint-disable no-undef */
7-
// jscs:disable jsDoc
6+
/* global grecaptcha */
87
define(
98
[
109
'uiComponent',
1110
'jquery',
1211
'ko',
12+
'underscore',
1313
'Magento_ReCaptchaFrontendUi/js/registry',
14-
'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader'
15-
],
16-
function (Component, $, ko, registry, reCaptchaLoader, undefined) {
14+
'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader',
15+
'Magento_ReCaptchaFrontendUi/js/nonInlineReCaptchaRenderer'
16+
], function (Component, $, ko, _, registry, reCaptchaLoader, nonInlineReCaptchaRenderer) {
1717
'use strict';
1818

1919
return Component.extend({
@@ -22,8 +22,10 @@ define(
2222
template: 'Magento_ReCaptchaFrontendUi/reCaptcha',
2323
reCaptchaId: 'recaptcha'
2424
},
25-
_isApiRegistered: undefined,
2625

26+
/**
27+
* @inheritdoc
28+
*/
2729
initialize: function () {
2830
this._super();
2931
this._loadApi();
@@ -75,8 +77,7 @@ define(
7577
* Initialize reCAPTCHA after first rendering
7678
*/
7779
initCaptcha: function () {
78-
var me = this,
79-
$parentForm,
80+
var $parentForm,
8081
$wrapper,
8182
$reCaptcha,
8283
widgetId,
@@ -102,21 +103,24 @@ define(
102103
$reCaptcha.attr('id', this.getReCaptchaId());
103104

104105
$parentForm = $wrapper.parents('form');
105-
me = this;
106106

107107
parameters = _.extend(
108108
{
109109
'callback': function (token) { // jscs:ignore jsDoc
110-
me.reCaptchaCallback(token);
111-
me.validateReCaptcha(true);
112-
},
110+
this.reCaptchaCallback(token);
111+
this.validateReCaptcha(true);
112+
}.bind(this),
113113
'expired-callback': function () {
114-
me.validateReCaptcha(false);
115-
}
114+
this.validateReCaptcha(false);
115+
}.bind(this)
116116
},
117117
this.settings.rendering
118118
);
119119

120+
if (parameters.size === 'invisible' && parameters.badge !== 'inline') {
121+
nonInlineReCaptchaRenderer.add($reCaptcha, parameters);
122+
}
123+
120124
// eslint-disable-next-line no-undef
121125
widgetId = grecaptcha.render(this.getReCaptchaId(), parameters);
122126
this.initParentForm($parentForm, widgetId);
@@ -134,18 +138,17 @@ define(
134138
* @param {String} widgetId
135139
*/
136140
initParentForm: function (parentForm, widgetId) {
137-
var me = this,
138-
listeners;
141+
var listeners;
139142

140143
if (this.getIsInvisibleRecaptcha() && parentForm.length > 0) {
141144
parentForm.submit(function (event) {
142-
if (!me.tokenField.value) {
145+
if (!this.tokenField.value) {
143146
// eslint-disable-next-line no-undef
144147
grecaptcha.execute(widgetId);
145148
event.preventDefault(event);
146149
event.stopImmediatePropagation();
147150
}
148-
});
151+
}.bind(this));
149152

150153
// Move our (last) handler topmost. We need this to avoid submit bindings with ko.
151154
listeners = $._data(parentForm[0], 'events').submit;
@@ -160,6 +163,11 @@ define(
160163
}
161164
},
162165

166+
/**
167+
* Validates reCAPTCHA
168+
* @param {*} state
169+
* @returns {jQuery}
170+
*/
163171
validateReCaptcha: function (state) {
164172
if (!this.getIsInvisibleRecaptcha()) {
165173
return $(document).find('input[type=checkbox].required-captcha').prop('checked', state);
@@ -170,14 +178,12 @@ define(
170178
* Render reCAPTCHA
171179
*/
172180
renderReCaptcha: function () {
173-
var me = this;
174-
175181
if (window.grecaptcha && window.grecaptcha.render) { // Check if reCAPTCHA is already loaded
176-
me.initCaptcha();
182+
this.initCaptcha();
177183
} else { // Wait for reCAPTCHA to be loaded
178184
$(window).on('recaptchaapiready', function () {
179-
me.initCaptcha();
180-
});
185+
this.initCaptcha();
186+
}.bind(this));
181187
}
182188
},
183189

@@ -189,5 +195,4 @@ define(
189195
return this.reCaptchaId;
190196
}
191197
});
192-
}
193-
);
198+
});

ReCaptchaNewsletter/etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<group id="type_for">
1313
<field id="newsletter" translate="label comment" type="select" sortOrder="160" showInDefault="1"
1414
showInWebsite="1" showInStore="0" canRestore="1">
15-
<label>Enable Invisible reCAPTCHA in Newsletter Subscription</label>
15+
<label>Enable for Newsletter Subscription</label>
1616
<comment>If enabled, a badge will be displayed in every page.</comment>
1717
<source_model>Magento\ReCaptchaAdminUi\Model\OptionSource\Type</source_model>
1818
</field>

ReCaptchaNewsletter/view/frontend/layout/default.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
ifconfig="recaptcha_frontend/type_for/newsletter">
1818
<arguments>
1919
<argument name="recaptcha_for" xsi:type="string">newsletter</argument>
20-
<argument name="captcha_ui_config" xsi:type="array">
21-
<item name="invisible" xsi:type="boolean">true</item>
22-
<item name="rendering" xsi:type="array">
23-
<item name="badge" xsi:type="string">bottomright</item>
24-
<item name="size" xsi:type="string">invisible</item>
25-
</item>
26-
</argument>
2720
<argument name="jsLayout" xsi:type="array">
2821
<item name="components" xsi:type="array">
2922
<item name="recaptcha" xsi:type="array">
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
.block.newsletter {
7+
.field-recaptcha {
8+
.field {
9+
.control {
10+
&:before {
11+
content: none;
12+
}
13+
}
14+
}
15+
}
16+
}

ReCaptchaVersion2Checkbox/etc/adminhtml/system.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
18
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
310
<system>
@@ -6,9 +13,10 @@
613
showInStore="0">
714
<label>reCAPTCHA v2 ("I am not a robot")</label>
815

9-
<field id="public_key" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0"
16+
<field id="public_key" translate="label" type="obscure" sortOrder="10" showInDefault="1" showInWebsite="0"
1017
showInStore="0" canRestore="0">
1118
<label>Google API Website Key</label>
19+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
1220
</field>
1321

1422
<field id="private_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="0"
@@ -49,9 +57,10 @@
4957
showInStore="1">
5058
<label>reCAPTCHA v2 ("I am not a robot")</label>
5159

52-
<field id="public_key" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
60+
<field id="public_key" translate="label" type="obscure" sortOrder="10" showInDefault="1" showInWebsite="1"
5361
showInStore="0" canRestore="0">
5462
<label>Google API Website Key</label>
63+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
5564
</field>
5665

5766
<field id="private_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="1"

ReCaptchaVersion2Checkbox/etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<default>
1111
<recaptcha_backend>
1212
<type_recaptcha>
13+
<public_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
1314
<private_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
1415
<size>normal</size>
1516
<theme>light</theme>
@@ -19,6 +20,7 @@
1920
</recaptcha_backend>
2021
<recaptcha_frontend>
2122
<type_recaptcha>
23+
<public_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2224
<private_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2325
<size>normal</size>
2426
<theme>light</theme>

ReCaptchaVersion2Invisible/etc/adminhtml/system.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
18
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
310
<system>
@@ -6,9 +13,10 @@
613
showInStore="0">
714
<label>reCAPTCHA v2 Invisible</label>
815

9-
<field id="public_key" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0"
16+
<field id="public_key" translate="label" type="obscure" sortOrder="10" showInDefault="1" showInWebsite="0"
1017
showInStore="0" canRestore="0">
1118
<label>Google API Website Key</label>
19+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
1220
</field>
1321

1422
<field id="private_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="0"
@@ -49,9 +57,10 @@
4957
showInStore="1">
5058
<label>reCAPTCHA v2 Invisible</label>
5159

52-
<field id="public_key" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
60+
<field id="public_key" translate="label" type="obscure" sortOrder="10" showInDefault="1" showInWebsite="1"
5361
showInStore="0" canRestore="0">
5462
<label>Google API Website Key</label>
63+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
5564
</field>
5665

5766
<field id="private_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="1"

ReCaptchaVersion2Invisible/etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<default>
1111
<recaptcha_backend>
1212
<type_invisible>
13+
<public_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
1314
<private_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
1415
<position>inline</position>
1516
<theme>light</theme>
@@ -19,6 +20,7 @@
1920
</recaptcha_backend>
2021
<recaptcha_frontend>
2122
<type_invisible>
23+
<public_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2224
<private_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2325
<position>inline</position>
2426
<theme>light</theme>

ReCaptchaVersion3Invisible/etc/adminhtml/system.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
18
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
310
<system>
@@ -6,9 +13,10 @@
613
showInStore="0">
714
<label>reCAPTCHA v3 Invisible</label>
815

9-
<field id="public_key" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0"
16+
<field id="public_key" translate="label" type="obscure" sortOrder="10" showInDefault="1" showInWebsite="0"
1017
showInStore="0" canRestore="0">
1118
<label>Google API Website Key</label>
19+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
1220
</field>
1321

1422
<field id="private_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="0"
@@ -59,9 +67,10 @@
5967
showInStore="1">
6068
<label>reCAPTCHA v3 Invisible</label>
6169

62-
<field id="public_key" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
70+
<field id="public_key" translate="label" type="obscure" sortOrder="10" showInDefault="1" showInWebsite="1"
6371
showInStore="0" canRestore="0">
6472
<label>Google API Website Key</label>
73+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
6574
</field>
6675

6776
<field id="private_key" translate="label" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="1"

0 commit comments

Comments
 (0)