Skip to content

Commit 9981e12

Browse files
authored
Merge pull request #783 from CleanTalk/integ_bloom_av
Integ bloom av
2 parents 64da8de + 13d3fbd commit 9981e12

File tree

3 files changed

+242
-2
lines changed

3 files changed

+242
-2
lines changed

inc/cleantalk-pluggable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ function apbct_is_skip_request($ajax = false, $ajax_message_obj = array())
14501450
apbct_is_plugin_active('bloom/bloom.php') &&
14511451
Post::get('action') === 'bloom_subscribe'
14521452
) {
1453-
return 'Bloom';
1453+
return 'Bloom skip - has the direct integration';
14541454
}
14551455

14561456
// Ajax Search Lite - these requests will be caught by search form protection

lib/Cleantalk/Antispam/Integrations/BloomForms.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@ public function getDataForChecking($argument)
1010
{
1111
Cookie::$force_alt_cookies_global = true;
1212

13-
return ct_gfa(apply_filters('apbct__filter_post', $_POST));
13+
$filtered_post = apply_filters('apbct__filter_post', $_POST);
14+
$nickname = '';
15+
16+
// Extract nickname from subscribe_data_array if it exists
17+
if ( ! empty($_POST['subscribe_data_array']) && is_string($_POST['subscribe_data_array']) ) {
18+
// Remove escaping from quoted JSON
19+
$data_to_decode = wp_unslash($_POST['subscribe_data_array']);
20+
if ( is_string($data_to_decode) ) {
21+
$subscribe_data = json_decode($data_to_decode, true);
22+
if ( is_array($subscribe_data) && ! empty($subscribe_data['name']) ) {
23+
$nickname = sanitize_text_field($subscribe_data['name']);
24+
}
25+
}
26+
}
27+
28+
return ct_gfa_dto($filtered_post, '', $nickname)->getArray();
1429
}
1530

1631
public function doBlock($message)
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php
2+
3+
namespace Cleantalk\Antispam\Integrations;
4+
5+
use Cleantalk\ApbctWP\Variables\Post;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class TestBloomForms extends TestCase
9+
{
10+
private $integration;
11+
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
$this->integration = new BloomForms();
16+
}
17+
18+
protected function tearDown(): void
19+
{
20+
// Clean up global state
21+
$_POST = [];
22+
Post::getInstance()->variables = [];
23+
parent::tearDown();
24+
}
25+
26+
/**
27+
* Test extraction of nickname from subscribe_data_array with valid JSON
28+
*/
29+
public function testGetDataForCheckingWithValidJsonName()
30+
{
31+
$_POST['action'] = 'bloom_subscribe';
32+
$_POST['subscribe_data_array'] = '{"list_id":123,"name":"TestUser","email":"test@example.com"}';
33+
34+
$result = $this->integration->getDataForChecking(null);
35+
36+
$this->assertIsArray($result);
37+
$this->assertEquals('TestUser', $result['nickname']);
38+
}
39+
40+
/**
41+
* Test extraction of nickname from subscribe_data_array with escaped JSON (real-world case)
42+
*/
43+
public function testGetDataForCheckingWithEscapedJsonName()
44+
{
45+
$_POST['action'] = 'bloom_subscribe';
46+
$_POST['subscribe_data_array'] = '{\"list_id\":113164156,\"account_name\":\"test@example\",\"service\":\"mailerlite\",\"name\":\"qwe123\",\"email\":\"test@example.com\",\"page_id\":245335,\"optin_id\":\"optin_40\",\"last_name\":\"\",\"ip_address\":\"true\"}';
47+
48+
$result = $this->integration->getDataForChecking(null);
49+
50+
$this->assertIsArray($result);
51+
$this->assertEquals('qwe123', $result['nickname']);
52+
}
53+
54+
/**
55+
* Test with empty name in subscribe_data_array
56+
*/
57+
public function testGetDataForCheckingWithEmptyName()
58+
{
59+
$_POST['action'] = 'bloom_subscribe';
60+
$_POST['subscribe_data_array'] = '{"list_id":123,"name":"","email":"test@example.com"}';
61+
62+
$result = $this->integration->getDataForChecking(null);
63+
64+
$this->assertIsArray($result);
65+
$this->assertEquals('', $result['nickname']);
66+
}
67+
68+
/**
69+
* Test without subscribe_data_array field
70+
*/
71+
public function testGetDataForCheckingWithoutSubscribeDataArray()
72+
{
73+
$_POST['action'] = 'bloom_subscribe';
74+
$_POST['email'] = 'test@example.com';
75+
76+
$result = $this->integration->getDataForChecking(null);
77+
78+
$this->assertIsArray($result);
79+
$this->assertEquals('', $result['nickname']);
80+
}
81+
82+
/**
83+
* Test with invalid JSON in subscribe_data_array
84+
*/
85+
public function testGetDataForCheckingWithInvalidJson()
86+
{
87+
$_POST['action'] = 'bloom_subscribe';
88+
$_POST['subscribe_data_array'] = 'not a valid json string';
89+
90+
$result = $this->integration->getDataForChecking(null);
91+
92+
$this->assertIsArray($result);
93+
$this->assertEquals('', $result['nickname']);
94+
}
95+
96+
/**
97+
* Test with special characters in name
98+
*/
99+
public function testGetDataForCheckingWithSpecialCharactersInName()
100+
{
101+
$_POST['action'] = 'bloom_subscribe';
102+
$_POST['subscribe_data_array'] = '{"name":"O\'Brien Müller","email":"special@example.com"}';
103+
104+
$result = $this->integration->getDataForChecking(null);
105+
106+
$this->assertIsArray($result);
107+
$this->assertStringContainsString("O'Brien", $result['nickname']);
108+
$this->assertStringContainsString('Müller', $result['nickname']);
109+
}
110+
111+
/**
112+
* Test with name containing only whitespace
113+
*/
114+
public function testGetDataForCheckingWithWhitespaceName()
115+
{
116+
$_POST['action'] = 'bloom_subscribe';
117+
$_POST['subscribe_data_array'] = '{"name":" ","email":"test@example.com"}';
118+
119+
$result = $this->integration->getDataForChecking(null);
120+
121+
$this->assertIsArray($result);
122+
// sanitize_text_field trims whitespace, so it should be empty
123+
$this->assertEquals('', $result['nickname']);
124+
}
125+
126+
/**
127+
* Test with numeric name value
128+
*/
129+
public function testGetDataForCheckingWithNumericName()
130+
{
131+
$_POST['action'] = 'bloom_subscribe';
132+
$_POST['subscribe_data_array'] = '{"name":12345,"email":"test@example.com"}';
133+
134+
$result = $this->integration->getDataForChecking(null);
135+
136+
$this->assertIsArray($result);
137+
// Numeric value should be converted to string
138+
$this->assertEquals('12345', $result['nickname']);
139+
}
140+
141+
/**
142+
* Test with null name value in JSON
143+
*/
144+
public function testGetDataForCheckingWithNullName()
145+
{
146+
$_POST['action'] = 'bloom_subscribe';
147+
$_POST['subscribe_data_array'] = '{"name":null,"email":"test@example.com"}';
148+
149+
$result = $this->integration->getDataForChecking(null);
150+
151+
$this->assertIsArray($result);
152+
$this->assertEquals('', $result['nickname']);
153+
}
154+
155+
/**
156+
* Test with missing name field in JSON
157+
*/
158+
public function testGetDataForCheckingWithMissingNameField()
159+
{
160+
$_POST['action'] = 'bloom_subscribe';
161+
$_POST['subscribe_data_array'] = '{"email":"test@example.com","list_id":123}';
162+
163+
$result = $this->integration->getDataForChecking(null);
164+
165+
$this->assertIsArray($result);
166+
$this->assertEquals('', $result['nickname']);
167+
}
168+
169+
/**
170+
* Test with empty POST data
171+
*/
172+
public function testGetDataForCheckingWithEmptyPost()
173+
{
174+
$_POST = [];
175+
176+
$result = $this->integration->getDataForChecking(null);
177+
178+
$this->assertIsArray($result);
179+
$this->assertEquals('', $result['nickname']);
180+
}
181+
182+
/**
183+
* Test with array value for subscribe_data_array (should not decode)
184+
*/
185+
public function testGetDataForCheckingWithArraySubscribeData()
186+
{
187+
$_POST['action'] = 'bloom_subscribe';
188+
$_POST['subscribe_data_array'] = ['name' => 'TestUser', 'email' => 'test@example.com'];
189+
190+
$result = $this->integration->getDataForChecking(null);
191+
192+
$this->assertIsArray($result);
193+
// Array is not a string, so nickname extraction should not work
194+
$this->assertEquals('', $result['nickname']);
195+
}
196+
197+
/**
198+
* Test with double-escaped JSON
199+
*/
200+
public function testGetDataForCheckingWithDoubleEscapedJson()
201+
{
202+
$_POST['action'] = 'bloom_subscribe';
203+
// Double escaped - after wp_unslash should become valid JSON
204+
$_POST['subscribe_data_array'] = '{\\\"name\\\":\\\"DoubleEscaped\\\",\\\"email\\\":\\\"test@example.com\\\"}';
205+
206+
$result = $this->integration->getDataForChecking(null);
207+
208+
$this->assertIsArray($result);
209+
// This depends on how wp_unslash handles double escaping
210+
}
211+
212+
/**
213+
* Test email extraction from subscribe_data_array
214+
*/
215+
public function testGetDataForCheckingEmailExtraction()
216+
{
217+
$_POST['action'] = 'bloom_subscribe';
218+
$_POST['subscribe_data_array'] = '{"name":"TestUser","email":"bloom@example.com"}';
219+
220+
$result = $this->integration->getDataForChecking(null);
221+
222+
$this->assertIsArray($result);
223+
$this->assertArrayHasKey('email', $result);
224+
}
225+
}

0 commit comments

Comments
 (0)