Skip to content

Commit 16e3557

Browse files
committed
Update code according to the review comment
Handle Captcha JS using LayoutProcessor for checkout Add a condition in customer login to prevent captcha related JS
1 parent 047da16 commit 16e3557

File tree

5 files changed

+81
-77
lines changed

5 files changed

+81
-77
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 CaptchaLayoutProcessor 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+
if (isset($jsLayout['components']['checkout']['children']['authentication']['children']['captcha'])) {
33+
unset($jsLayout['components']['checkout']['children']['authentication']['children']['captcha']);
34+
}
35+
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['customer-email']['children']['additional-login-form-fields']['children']['captcha'])) {
36+
unset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['customer-email']['children']['additional-login-form-fields']['children']['captcha']);
37+
}
38+
if (isset($jsLayout['components']['authenticationPopup']['children']['captcha'])) {
39+
unset($jsLayout['components']['authenticationPopup']['children']['captcha']);
40+
}
41+
}
42+
return $jsLayout;
43+
}
44+
}

app/code/Magento/Captcha/Plugin/CheckCaptchaOnStorefront.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
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-
<type name="Magento\Customer\Block\Account\AuthenticationPopup">
41-
<plugin name="check_captcha_on_storefront" type="Magento\Captcha\Plugin\CheckCaptchaOnStorefront" sortOrder="10"/>
40+
41+
<type name="Magento\Checkout\Block\Onepage">
42+
<arguments>
43+
<argument name="layoutProcessors" xsi:type="array">
44+
<item name="captcha_processor" xsi:type="object">Magento\Captcha\Block\CaptchaLayoutProcessor</item>
45+
</argument>
46+
</arguments>
4247
</type>
4348
</config>

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe.
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Customer\Block\Account;
77

88
use Magento\Customer\Model\Form;
99
use Magento\Store\Model\ScopeInterface;
10+
use Magento\Customer\Model\Context;
1011

1112
/**
1213
* @api
@@ -48,6 +49,16 @@ public function __construct(
4849
*/
4950
public function getJsLayout()
5051
{
52+
// Check if captcha is not enabled and user is not logged in
53+
if (!$this->_scopeConfig->getValue(
54+
Form::XML_PATH_CUSTOMER_CAPTCHA_ENABLED,
55+
ScopeInterface::SCOPE_STORE
56+
) && !$this->isLoggedIn()) {
57+
if(isset($this->jsLayout['components']['authenticationPopup']['children']['captcha'])) {
58+
unset($this->jsLayout['components']['authenticationPopup']['children']['captcha']);
59+
}
60+
}
61+
5162
return $this->serializer->serialize($this->jsLayout);
5263
}
5364

@@ -122,4 +133,14 @@ public function getCustomerForgotPasswordUrl()
122133
{
123134
return $this->getUrl('customer/account/forgotpassword');
124135
}
136+
137+
/**
138+
* Is logged in
139+
*
140+
* @return bool
141+
*/
142+
private function isLoggedIn(): ?bool
143+
{
144+
return $this->httpContext->getValue(Context::CONTEXT_AUTH);
145+
}
125146
}

app/code/Magento/Customer/Model/Form.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe.
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Customer\Model;
@@ -16,6 +16,11 @@ class Form extends \Magento\Eav\Model\Form
1616
*/
1717
public const XML_PATH_ENABLE_AUTOCOMPLETE = 'customer/password/autocomplete_on_storefront';
1818

19+
/**
20+
* XML configuration paths for "Enable CAPTCHA on Storefront" property
21+
*/
22+
public const XML_PATH_CUSTOMER_CAPTCHA_ENABLED = 'customer/captcha/enable';
23+
1924
/**
2025
* Current module pathname
2126
*

0 commit comments

Comments
 (0)