Skip to content

Commit 47ae15c

Browse files
authored
Merge pull request #65 from /issues/64
Introduce ReCaptchaSendFriend module
2 parents 3d67ab4 + ba1e04f commit 47ae15c

File tree

21 files changed

+355
-62
lines changed

21 files changed

+355
-62
lines changed

ReCaptcha/Model/Config.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Read configuration from store config
1717
*/
18-
class Config
18+
class Config implements ConfigInterface
1919
{
2020
public const XML_PATH_ENABLED_BACKEND = 'recaptcha/backend/enabled';
2121
public const XML_PATH_ENABLED_FRONTEND = 'recaptcha/frontend/enabled';
@@ -41,7 +41,6 @@ class Config
4141
public const XML_PATH_ENABLED_FRONTEND_CREATE = 'recaptcha/frontend/enabled_create';
4242
public const XML_PATH_ENABLED_FRONTEND_REVIEW = 'recaptcha/frontend/enabled_review';
4343
public const XML_PATH_ENABLED_FRONTEND_NEWSLETTER = 'recaptcha/frontend/enabled_newsletter';
44-
public const XML_PATH_ENABLED_FRONTEND_SENDFRIEND = 'recaptcha/frontend/enabled_sendfriend';
4544

4645
/**
4746
* @var ScopeConfigInterface
@@ -93,7 +92,7 @@ public function getPrivateKey(): string
9392
*/
9493
public function isEnabledBackend(): bool
9594
{
96-
if (!$this->getPrivateKey() || !$this->getPublicKey()) {
95+
if (!$this->isAreaEnabled(Area::AREA_ADMINHTML) || !$this->getPrivateKey() || !$this->getPublicKey()) {
9796
return false;
9897
}
9998

@@ -106,7 +105,7 @@ public function isEnabledBackend(): bool
106105
*/
107106
public function isEnabledFrontend(): bool
108107
{
109-
if (!$this->getPrivateKey() || !$this->getPublicKey()) {
108+
if (!$this->isAreaEnabled(Area::AREA_FRONTEND) || !$this->getPrivateKey() || !$this->getPublicKey()) {
110109
return false;
111110
}
112111

@@ -212,22 +211,6 @@ public function isEnabledFrontendNewsletter(): bool
212211
);
213212
}
214213

215-
/**
216-
* Return true if enabled on frontend send to friend
217-
* @return bool
218-
*/
219-
public function isEnabledFrontendSendFriend(): bool
220-
{
221-
if (!$this->isEnabledFrontend()) {
222-
return false;
223-
}
224-
225-
return (bool) $this->scopeConfig->getValue(
226-
static::XML_PATH_ENABLED_FRONTEND_SENDFRIEND,
227-
ScopeInterface::SCOPE_WEBSITE
228-
);
229-
}
230-
231214
/**
232215
* @return bool
233216
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptcha\Model;
9+
10+
/**
11+
* Extension point of the ReCaptcha configuration
12+
*
13+
* @api
14+
*/
15+
interface ConfigEnabledInterface
16+
{
17+
/**
18+
* Return true if functionality of corresponding point is enabled in configuration
19+
* @return bool
20+
*/
21+
public function isEnabled(): bool;
22+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptcha\Model;
9+
10+
use Magento\Framework\Phrase;
11+
12+
/**
13+
* Represents general ReCaptcha configuration
14+
*
15+
* @api
16+
*/
17+
interface ConfigInterface
18+
{
19+
/**
20+
* Get error
21+
* @return Phrase
22+
*/
23+
public function getErrorDescription(): Phrase;
24+
25+
/**
26+
* Get google recaptcha public key
27+
* @return string
28+
*/
29+
public function getPublicKey(): string;
30+
31+
/**
32+
* Get google recaptcha private key
33+
* @return string
34+
*/
35+
public function getPrivateKey(): string;
36+
37+
/**
38+
* Return true if enabled on backend
39+
* @return bool
40+
*/
41+
public function isEnabledBackend(): bool;
42+
43+
/**
44+
* Return true if enabled on frontend
45+
* @return bool
46+
*/
47+
public function isEnabledFrontend(): bool;
48+
49+
/**
50+
* @return bool
51+
*/
52+
public function isInvisibleRecaptcha(): bool;
53+
54+
/**
55+
* Get data size
56+
* @return string
57+
*/
58+
public function getFrontendSize(): string;
59+
60+
/**
61+
* Get data size
62+
* @return string
63+
*/
64+
public function getBackendSize(): string;
65+
66+
/**
67+
* Get data size
68+
* @return string
69+
*/
70+
public function getFrontendTheme(): ?string;
71+
72+
/**
73+
* Get data size
74+
* @return string
75+
*/
76+
public function getBackendTheme(): string;
77+
78+
/**
79+
* Get data size
80+
* @return string
81+
*/
82+
public function getFrontendPosition(): ?string;
83+
84+
/**
85+
* Get reCaptcha type
86+
* @return string
87+
*/
88+
public function getType(): string;
89+
90+
/**
91+
* Get language code
92+
* @return string
93+
*/
94+
public function getLanguageCode(): string;
95+
96+
/**
97+
* Get minimum frontend score
98+
* @return float
99+
*/
100+
public function getMinFrontendScore(): float;
101+
102+
/**
103+
* Get minimum frontend score
104+
* @return float
105+
*/
106+
public function getMinBackendScore(): float;
107+
}

ReCaptcha/Model/LayoutSettings.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88
namespace Magento\ReCaptcha\Model;
99

10+
use Magento\ReCaptcha\Model\ConfigEnabledInterface;
11+
1012
/**
11-
* Return the layout configuration setting for reCaptcha
13+
* Extension point of the layout configuration setting for reCaptcha
14+
*
15+
* @api
1216
*/
1317
class LayoutSettings
1418
{
@@ -17,13 +21,21 @@ class LayoutSettings
1721
*/
1822
private $config;
1923

24+
/**
25+
* @var ConfigEnabledInterface[]
26+
*/
27+
private $configEnabledProviders;
28+
2029
/**
2130
* @param Config $config
31+
* @param ConfigEnabledInterface[] $configEnabledProviders
2232
*/
2333
public function __construct(
24-
Config $config
34+
Config $config,
35+
array $configEnabledProviders
2536
) {
2637
$this->config = $config;
38+
$this->configEnabledProviders = $configEnabledProviders;
2739
}
2840

2941
/**
@@ -32,7 +44,7 @@ public function __construct(
3244
*/
3345
public function getCaptchaSettings(): array
3446
{
35-
return [
47+
$settings = [
3648
'siteKey' => $this->config->getPublicKey(),
3749
'size' => $this->config->getFrontendSize(),
3850
'badge' => $this->config->getFrontendPosition(),
@@ -45,8 +57,11 @@ public function getCaptchaSettings(): array
4557
'contact' => $this->config->isEnabledFrontendContact(),
4658
'review' => $this->config->isEnabledFrontendReview(),
4759
'newsletter' => $this->config->isEnabledFrontendNewsletter(),
48-
'sendfriend' => $this->config->isEnabledFrontendSendFriend(),
4960
]
5061
];
62+
foreach ($this->configEnabledProviders as $key => $configEnabledProvider) {
63+
$settings['enabled'][$key] = $configEnabledProvider->isEnabled();
64+
}
65+
return $settings;
5166
}
5267
}

ReCaptcha/etc/adminhtml/system.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
<field id="enabled">1</field>
173173
</depends>
174174
</field>
175-
<field id="enabled_newsletter" translate="label" type="select" sortOrder="250" showInDefault="1"
175+
<field id="enabled_newsletter" translate="label" type="select" sortOrder="250" showInDefault="1"
176176
showInWebsite="1" showInStore="0" canRestore="1">
177177
<label>Use invisible ReCaptcha in newsletter</label>
178178
<comment>Requires an Invisible ReCaptcha v2 or ReCaptcha v3 key. If enabled, a badge will be displayed in every page.</comment>
@@ -181,14 +181,6 @@
181181
<field id="enabled">1</field>
182182
</depends>
183183
</field>
184-
<field id="enabled_sendfriend" translate="label" type="select" sortOrder="260" showInDefault="1"
185-
showInWebsite="1" showInStore="0" canRestore="1">
186-
<label>Use in Send To Friend</label>
187-
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
188-
<depends>
189-
<field id="enabled">1</field>
190-
</depends>
191-
</field>
192184
</group>
193185
</section>
194186
</system>

ReCaptcha/etc/config.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
<enabled_create>1</enabled_create>
3131
<enabled_review>1</enabled_review>
3232
<enabled_newsletter>1</enabled_newsletter>
33-
<enabled_sendfriend>1</enabled_sendfriend>
3433
<min_score>0.6</min_score>
3534
</frontend>
3635
</recaptcha>

ReCaptcha/etc/di.xml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<preference for="Magento\ReCaptcha\Model\ConfigInterface" type="Magento\ReCaptcha\Model\Config"/>
1011
<preference for="Magento\ReCaptcha\Model\ValidateInterface" type="Magento\ReCaptcha\Model\Validate"/>
12+
<preference for="Magento\ReCaptcha\Model\CaptchaRequestHandlerInterface"
13+
type="Magento\ReCaptcha\Model\CaptchaRequestHandler"/>
1114

1215
<type name="Magento\Framework\Console\CommandListInterface">
1316
<arguments>
@@ -21,18 +24,6 @@
2124
<plugin name="exclude-recaptcha-from-minification" type="Magento\ReCaptcha\Plugin\ExcludeFromMinification"/>
2225
</type>
2326

24-
<type name="Magento\ReCaptcha\Block\Frontend\ReCaptcha">
25-
<arguments>
26-
<!-- Deprecated parameter encoder -->
27-
<argument name="encoder" xsi:type="null" />
28-
<!-- Deprecated parameter decoder -->
29-
<argument name="decoder" xsi:type="null" />
30-
</arguments>
31-
</type>
32-
33-
<preference for="Magento\ReCaptcha\Model\CaptchaRequestHandlerInterface"
34-
type="Magento\ReCaptcha\Model\CaptchaRequestHandler"/>
35-
3627
<!-- Set sensitive information -->
3728
<type name="Magento\Config\Model\Config\TypePool">
3829
<arguments>

ReCaptcha/etc/frontend/events.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,4 @@
2929
<event name="controller_action_predispatch_newsletter_subscriber_new">
3030
<observer name="recaptcha_on_newsletter" instance="Magento\ReCaptcha\Observer\Frontend\NewsletterObserver" />
3131
</event>
32-
<event name="controller_action_predispatch_sendfriend_product_sendmail">
33-
<observer name="recaptcha_on_send_to_a_friend" instance="Magento\ReCaptcha\Observer\Frontend\SendFriendObserver" />
34-
</event>
3532
</config>

ReCaptcha/view/frontend/web/css/source/_module.less

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010

1111
.required-captcha.checkbox{
12-
position: absolute;
12+
position: absolute;
1313
display: block;
1414
visibility: visible;
1515
overflow: hidden;
@@ -23,7 +23,3 @@
2323
margin-bottom: 10px;
2424
}
2525
}
26-
27-
.form.send.friend .g-recaptcha {
28-
margin-top: 40px;
29-
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaSendFriend\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\ReCaptcha\Model\ConfigEnabledInterface;
12+
use Magento\ReCaptcha\Model\ConfigInterface;
13+
use Magento\Store\Model\ScopeInterface;
14+
15+
/**
16+
* Read configuration from store config
17+
*/
18+
class ConfigEnabled implements ConfigEnabledInterface
19+
{
20+
public const XML_PATH_ENABLED_FRONTEND_SENDFRIEND = 'recaptcha/frontend/enabled_sendfriend';
21+
22+
/**
23+
* @var ConfigInterface
24+
*/
25+
private $reCaptchaConfig;
26+
27+
/**
28+
* @var ScopeConfigInterface
29+
*/
30+
private $scopeConfig;
31+
32+
/**
33+
* @param ConfigInterface $reCaptchaConfig
34+
* @param ScopeConfigInterface $scopeConfig
35+
*/
36+
public function __construct(
37+
ConfigInterface $reCaptchaConfig,
38+
ScopeConfigInterface $scopeConfig
39+
) {
40+
$this->reCaptchaConfig = $reCaptchaConfig;
41+
$this->scopeConfig = $scopeConfig;
42+
}
43+
44+
/**
45+
* Return true if enabled on frontend send to friend
46+
* @return bool
47+
*/
48+
public function isEnabled(): bool
49+
{
50+
if (!$this->reCaptchaConfig->isEnabledFrontend()) {
51+
return false;
52+
}
53+
54+
return (bool)$this->scopeConfig->getValue(
55+
static::XML_PATH_ENABLED_FRONTEND_SENDFRIEND,
56+
ScopeInterface::SCOPE_WEBSITE
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)