Skip to content

Commit 5edd1a4

Browse files
committed
Fix review comments
1 parent df37b87 commit 5edd1a4

File tree

7 files changed

+109
-88
lines changed

7 files changed

+109
-88
lines changed

app/code/Magento/Captcha/Block/CaptchaLayoutProcessor.php

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe.
4+
* All Rights Reserved.
5+
*/
6+
7+
declare(strict_types=1);
8+
namespace Magento\Captcha\Block;
9+
10+
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
11+
use Magento\Captcha\Helper\Data as HelperCaptcha;
12+
13+
class CheckoutLayoutProcessor implements LayoutProcessorInterface
14+
{
15+
/**
16+
* @param HelperCaptcha $helper
17+
*/
18+
public function __construct(
19+
private readonly HelperCaptcha $helper
20+
) {
21+
}
22+
23+
/**
24+
* Remove captcha from checkout page if it is disabled
25+
*
26+
* @param array $jsLayout
27+
* @return array
28+
*/
29+
public function process($jsLayout): array
30+
{
31+
if ($this->helper->getConfig('enable')) {
32+
$captcha = [
33+
'component' => 'Magento_Captcha/js/view/checkout/loginCaptcha',
34+
'displayArea' => 'additional-login-form-fields',
35+
'formId' => 'user_login',
36+
'configSource' => 'checkoutConfig'
37+
];
38+
$jsLayout['components']['checkout']['children']['authentication']['children']['captcha'] = $captcha;
39+
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
40+
['shippingAddress']['children']['customer-email']['children']['additional-login-form-fields']
41+
['children']['captcha'] = $captcha;
42+
}
43+
return $jsLayout;
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace Magento\Captcha\Block;
3+
4+
use Magento\Framework\App\Config\ScopeConfigInterface;
5+
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
6+
7+
class LayoutProcessor implements LayoutProcessorInterface
8+
{
9+
const CAPTCHA_ENABLED_PATH = 'customer/captcha/enable'; // Captcha config path
10+
11+
/**
12+
* @var ScopeConfigInterface
13+
*/
14+
private $scopeConfig;
15+
16+
public function __construct(ScopeConfigInterface $scopeConfig)
17+
{
18+
die('LayoutProcessor');
19+
$this->scopeConfig = $scopeConfig;
20+
}
21+
22+
/**
23+
* Modify jsLayout to exclude Captcha if disabled.
24+
*
25+
* @param array $jsLayout
26+
* @return array
27+
*/
28+
public function process($jsLayout): array
29+
{
30+
// Check Captcha enable status
31+
$isCaptchaEnabled = $this->scopeConfig->isSetFlag(self::CAPTCHA_ENABLED_PATH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
32+
33+
if (!$isCaptchaEnabled) {
34+
// Remove Captcha-related components
35+
if (isset($jsLayout['components']['captcha'])) {
36+
unset($jsLayout['components']['captcha']);
37+
}
38+
}
39+
40+
return $jsLayout;
41+
}
42+
}

app/code/Magento/Captcha/etc/frontend/di.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@
3737
<type name="Magento\Sales\Api\OrderManagementInterface">
3838
<plugin name="reset_payment_attempts_after_order_is_placed_plugin" type="Magento\Captcha\Plugin\ResetPaymentAttemptsAfterOrderIsPlacedPlugin"/>
3939
</type>
40-
4140
<type name="Magento\Checkout\Block\Onepage">
4241
<arguments>
4342
<argument name="layoutProcessors" xsi:type="array">
44-
<item name="captcha_processor" xsi:type="object">Magento\Captcha\Block\CaptchaLayoutProcessor</item>
43+
<item name="checkout_processor" xsi:type="object">Magento\Captcha\Block\CheckoutLayoutProcessor</item>
44+
</argument>
45+
</arguments>
46+
</type>
47+
<type name="Magento\Customer\Block\Account\AuthenticationPopup">
48+
<arguments>
49+
<argument name="layoutProcessors" xsi:type="array">
50+
<item name="captcha_processor" xsi:type="object">Magento\Captcha\Block\Checkout\LayoutProcessor</item>
4551
</argument>
4652
</arguments>
4753
</type>

app/code/Magento/Captcha/view/frontend/layout/checkout_index_index.xml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
<item name="children" xsi:type="array">
1616
<item name="authentication" xsi:type="array">
1717
<item name="children" xsi:type="array">
18-
<item name="captcha" xsi:type="array">
19-
<item name="component" xsi:type="string">Magento_Captcha/js/view/checkout/loginCaptcha</item>
20-
<item name="displayArea" xsi:type="string">additional-login-form-fields</item>
21-
<item name="formId" xsi:type="string">user_login</item>
22-
<item name="configSource" xsi:type="string">checkoutConfig</item>
23-
</item>
18+
2419
</item>
2520
</item>
2621
<item name="steps" xsi:type="array">
@@ -33,12 +28,7 @@
3328
<item name="children" xsi:type="array">
3429
<item name="additional-login-form-fields" xsi:type="array">
3530
<item name="children" xsi:type="array">
36-
<item name="captcha" xsi:type="array">
37-
<item name="component" xsi:type="string">Magento_Captcha/js/view/checkout/loginCaptcha</item>
38-
<item name="displayArea" xsi:type="string">additional-login-form-fields</item>
39-
<item name="formId" xsi:type="string">user_login</item>
40-
<item name="configSource" xsi:type="string">checkoutConfig</item>
41-
</item>
31+
4232
</item>
4333
</item>
4434
</item>

app/code/Magento/Captcha/view/frontend/layout/default.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
<item name="components" xsi:type="array">
1414
<item name="authenticationPopup" xsi:type="array">
1515
<item name="children" xsi:type="array">
16-
<item name="captcha" xsi:type="array">
17-
<item name="component" xsi:type="string">Magento_Captcha/js/view/checkout/loginCaptcha</item>
18-
<item name="displayArea" xsi:type="string">additional-login-form-fields</item>
19-
<item name="formId" xsi:type="string">user_login</item>
20-
<item name="configSource" xsi:type="string">checkout</item>
21-
</item>
16+
2217
</item>
2318
</item>
2419
</item>

app/code/Magento/Customer/Block/Account/AuthenticationPopup.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,32 @@ class AuthenticationPopup extends \Magento\Framework\View\Element\Template
3232
*/
3333
private $httpContext;
3434

35+
/**
36+
* @var array
37+
*/
38+
private $layoutProcessors;
39+
3540
/**
3641
* @param \Magento\Framework\View\Element\Template\Context $context
3742
* @param array $data
3843
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
3944
* @param HttpContext $httpContext
45+
* @param array $layoutProcessors
4046
* @throws \RuntimeException
4147
*/
4248
public function __construct(
4349
\Magento\Framework\View\Element\Template\Context $context,
4450
array $data = [],
4551
?\Magento\Framework\Serialize\Serializer\Json $serializer = null,
46-
?HttpContext $httpContext = null
52+
?HttpContext $httpContext = null,
53+
array $layoutProcessors = []
4754
) {
4855
parent::__construct($context, $data);
4956
$this->jsLayout = isset($data['jsLayout']) && is_array($data['jsLayout']) ? $data['jsLayout'] : [];
5057
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
5158
->get(\Magento\Framework\Serialize\Serializer\Json::class);
5259
$this->httpContext = $httpContext ?: ObjectManager::getInstance()->get(HttpContext::class);
60+
$this->layoutProcessors = $layoutProcessors;
5361
}
5462

5563
/**
@@ -59,16 +67,9 @@ public function __construct(
5967
*/
6068
public function getJsLayout()
6169
{
62-
// Check if captcha is not enabled and user is not logged in
63-
if (!$this->_scopeConfig->getValue(
64-
Form::XML_PATH_CUSTOMER_CAPTCHA_ENABLED,
65-
ScopeInterface::SCOPE_STORE
66-
) && !$this->isLoggedIn()) {
67-
if (isset($this->jsLayout['components']['authenticationPopup']['children']['captcha'])) {
68-
unset($this->jsLayout['components']['authenticationPopup']['children']['captcha']);
69-
}
70+
foreach ($this->layoutProcessors as $processor) {
71+
$this->jsLayout = $processor->process($this->jsLayout);
7072
}
71-
7273
return $this->serializer->serialize($this->jsLayout);
7374
}
7475

@@ -143,14 +144,4 @@ public function getCustomerForgotPasswordUrl()
143144
{
144145
return $this->getUrl('customer/account/forgotpassword');
145146
}
146-
147-
/**
148-
* Is logged in
149-
*
150-
* @return bool
151-
*/
152-
private function isLoggedIn(): ?bool
153-
{
154-
return $this->httpContext->getValue(Context::CONTEXT_AUTH);
155-
}
156147
}

0 commit comments

Comments
 (0)