Skip to content

Commit 24a7c39

Browse files
Merge pull request #841 from CleanTalk/nsl_plugin_set_altsessions.ag
Upd. Integrations. Nextend Social Login: set altsession mode if Google auth provided and set up.
2 parents 7d4b7a6 + 71a6986 commit 24a7c39

4 files changed

Lines changed: 138 additions & 1 deletion

File tree

lib/Cleantalk/Antispam/Integrations/NextendSocialLogin.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function getDataForChecking($argument)
1717
)
1818
);
1919
}
20+
return null;
2021
}
2122

2223
public function doBlock($message)
@@ -25,4 +26,24 @@ public function doBlock($message)
2526
$ct_comment = $message;
2627
ct_die(null, null);
2728
}
29+
30+
/**
31+
* Checks if the OAuth provider is enabled by verifying the saved configuration.
32+
*
33+
* @param string $provider_id The ID of the OAuth provider.
34+
* @return bool Returns true if the OAuth provider is enabled and properly configured; otherwise, returns false.
35+
*/
36+
public static function isOAuthProviderEnabled($provider_id)
37+
{
38+
if ( ! $provider_id || ! is_string($provider_id) ) {
39+
return false;
40+
}
41+
try {
42+
$option = get_option($provider_id);
43+
$object = maybe_unserialize($option);
44+
return is_array($object) && !empty($object['client_secret']);
45+
} catch (\Throwable $e) {
46+
return false;
47+
}
48+
}
2849
}

lib/Cleantalk/ApbctWP/State.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AllowDynamicProperties;
66
use ArrayObject;
7+
use Cleantalk\Antispam\Integrations\NextendSocialLogin;
78
use Cleantalk\ApbctWP\FindSpam\LoginIPKeeper;
89
use Cleantalk\ApbctWP\Firewall\SFWUpdateSentinel;
910
use Cleantalk\ApbctWP\ServiceConstants;
@@ -1017,6 +1018,13 @@ public function isAltSessionsRequired($get_reason = false)
10171018
$result = 'plugin_active__moosend-email-marketing';
10181019
}
10191020

1021+
//nextend social login requires alt sessions https://app.doboard.com/1/task/53787
1022+
if (apbct_is_plugin_active('nextend-facebook-connect/nextend-facebook-connect.php')) {
1023+
if (NextendSocialLogin::isOAuthProviderEnabled('nsl_google')) {
1024+
$result = 'plugin_active__nextend-facebook-connect';
1025+
}
1026+
}
1027+
10201028
return $get_reason ? $result : $result !== false;
10211029
}
10221030

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace {
4+
if ( ! class_exists('NextendSocialProviderDummy', false) ) {
5+
class NextendSocialProviderDummy
6+
{
7+
private $auth_user_data = array();
8+
9+
public function __construct(array $auth_user_data = array())
10+
{
11+
$this->auth_user_data = $auth_user_data;
12+
}
13+
14+
public function getAuthUserData($key)
15+
{
16+
return $this->auth_user_data[$key] ?? null;
17+
}
18+
}
19+
}
20+
}
21+
22+
namespace Cleantalk\Antispam\Integrations {
23+
24+
if ( ! function_exists(__NAMESPACE__ . '\\ct_die') ) {
25+
function ct_die($message = null, $code = null)
26+
{
27+
$GLOBALS['nextend_social_login_test_ct_die'] = array(
28+
'message' => $message,
29+
'code' => $code,
30+
);
31+
}
32+
}
33+
34+
class TestNextendSocialLogin extends \ApbctTestCase
35+
{
36+
protected function setUp(): void
37+
{
38+
parent::setUp();
39+
$GLOBALS['nextend_social_login_test_ct_die'] = null;
40+
$GLOBALS['ct_comment'] = null;
41+
}
42+
43+
protected function tearDown(): void
44+
{
45+
unset($GLOBALS['nextend_social_login_test_ct_die']);
46+
$GLOBALS['ct_comment'] = null;
47+
parent::tearDown();
48+
}
49+
50+
public function testGetDataForCheckingReturnsEmailAndNicknameForNextendProvider()
51+
{
52+
$integration = new NextendSocialLogin();
53+
$provider = new \NextendSocialProviderDummy(array(
54+
'email' => 'user@example.com',
55+
'name' => 'User Name',
56+
));
57+
58+
$this->assertSame(
59+
array(
60+
'email' => 'user@example.com',
61+
'nickname' => 'User Name',
62+
),
63+
$integration->getDataForChecking($provider)
64+
);
65+
}
66+
67+
public function testGetDataForCheckingReturnsNullForUnsupportedArgument()
68+
{
69+
$integration = new NextendSocialLogin();
70+
71+
$this->assertNull($integration->getDataForChecking(new \stdClass()));
72+
}
73+
74+
public function testDoBlockSetsGlobalCommentAndInvokesCtDie()
75+
{
76+
$integration = new NextendSocialLogin();
77+
78+
$integration->doBlock('blocked message');
79+
80+
$this->assertSame('blocked message', $GLOBALS['ct_comment']);
81+
$this->assertSame(
82+
array(
83+
'message' => null,
84+
'code' => null,
85+
),
86+
$GLOBALS['nextend_social_login_test_ct_die']
87+
);
88+
}
89+
90+
public function testIsOAuthProviderEnabledReturnsFalseForInvalidProviderId()
91+
{
92+
$this->assertFalse(NextendSocialLogin::isOAuthProviderEnabled(''));
93+
$this->assertFalse(NextendSocialLogin::isOAuthProviderEnabled(123));
94+
$this->assertFalse(NextendSocialLogin::isOAuthProviderEnabled(null));
95+
}
96+
97+
public function testIsOAuthProviderEnabledReturnsTrueWhenClientSecretExists()
98+
{
99+
100+
update_option('nextend_social_login_provider', serialize(array(
101+
'client_secret' => 'secret-value',
102+
)));
103+
104+
$this->assertTrue(NextendSocialLogin::isOAuthProviderEnabled('nextend_social_login_provider'));
105+
}
106+
}
107+
108+
}

tests/ApbctTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class ApbctTestCase extends TestCase
77
{
88
/**
9-
* @var \CleantalkSP\SpbctWP\State
9+
* @var State
1010
*/
1111
protected static $state_storage;
1212

0 commit comments

Comments
 (0)