Skip to content

Commit 0a8de78

Browse files
authored
Merge pull request #847 from CleanTalk/integ_eventprime_av
New. Integration. Integration with Event Prime
2 parents a86e723 + 7f28f0a commit 0a8de78

4 files changed

Lines changed: 338 additions & 0 deletions

File tree

inc/cleantalk-integrations-by-hook.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@
516516
'ajax' => true,
517517
'ajax_and_post' => true
518518
),
519+
'EventPrime' => array(
520+
'hook' => ['ep_submit_register_form', 'ep_save_event_booking'],
521+
'setting' => 'forms__registrations_test',
522+
'ajax' => true,
523+
'ajax_and_post' => true
524+
),
519525
);
520526

521527
add_action('plugins_loaded', function () use ($apbct_active_integrations) {

inc/cleantalk-pluggable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,18 @@ class_exists('Cleantalk\Antispam\Integrations\CleantalkInternalForms')
17521752
) {
17531753
return 'Email Subscribers by Icegram Express - skip subscribe action';
17541754
}
1755+
1756+
// EventPrime - skip actions
1757+
if (
1758+
apbct_is_plugin_active('eventprime-event-calendar-management/event-prime.php') &&
1759+
(
1760+
Post::equal('action', 'ep_submit_register_form') ||
1761+
Post::equal('action', 'ep_rg_check_email') ||
1762+
Post::equal('action', 'ep_save_event_booking')
1763+
)
1764+
) {
1765+
return 'EventPrime - skip actions';
1766+
}
17551767
} else {
17561768
/*****************************************/
17571769
/* Here is non-ajax requests skipping */
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Cleantalk\Antispam\Integrations;
4+
5+
class EventPrime extends IntegrationBase
6+
{
7+
private $is_register_form;
8+
private $is_checkout_form;
9+
public function getDataForChecking($argument)
10+
{
11+
$this->is_register_form = isset($_POST['ep-attendee-register-nonce']);
12+
$this->is_checkout_form = isset($_POST['action']) && $_POST['action'] === 'ep_save_event_booking';
13+
if ($this->is_register_form || $this->is_checkout_form) {
14+
/**
15+
* Filter for POST
16+
*/
17+
$input_data = $_POST;
18+
19+
if ($this->is_checkout_form && !empty($input_data['data']) && is_string($input_data['data'])) {
20+
$parsed_data = array();
21+
parse_str($input_data['data'], $parsed_data);
22+
unset($input_data['data']);
23+
$input_data = array_merge($input_data, $parsed_data);
24+
}
25+
26+
if ($this->is_checkout_form && !isset($input_data['ep_rg_field_email'])) {
27+
return null;
28+
}
29+
30+
apbct_form__get_no_cookie_data($input_data);
31+
32+
$event_token = '';
33+
if (isset($input_data['ct_bot_detector_event_token'])) {
34+
$event_token = $input_data['ct_bot_detector_event_token'];
35+
unset($input_data['ct_bot_detector_event_token']);
36+
}
37+
if (isset($input_data['data']['ct_bot_detector_event_token'])) {
38+
$event_token = $input_data['data']['ct_bot_detector_event_token'];
39+
unset($input_data['data']['ct_bot_detector_event_token']);
40+
}
41+
42+
$processed_post = apply_filters('apbct__filter_post', $input_data);
43+
$data = ct_gfa_dto(
44+
$processed_post,
45+
$this->is_checkout_form &&
46+
isset($processed_post['ep_rg_field_email']) ?
47+
$processed_post['ep_rg_field_email'] :
48+
''
49+
)->getArray();
50+
51+
if (!empty($event_token)) {
52+
$data['event_token'] = $event_token;
53+
}
54+
55+
if ($this->is_register_form) {
56+
$data['register'] = true;
57+
}
58+
return $data;
59+
}
60+
61+
return null;
62+
}
63+
64+
public function doBlock($message)
65+
{
66+
if ($this->is_register_form) {
67+
wp_send_json([
68+
'data' => [
69+
'success' => 0,
70+
'msg' => $message
71+
],
72+
'success' => false,
73+
], 200);
74+
} elseif ($this->is_checkout_form) {
75+
die(
76+
json_encode(
77+
array(
78+
'apbct' => array(
79+
'blocked' => true,
80+
'comment' => $message,
81+
'stop_script' => apbct__stop_script_after_ajax_checking()
82+
)
83+
)
84+
)
85+
);
86+
}
87+
}
88+
}
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php
2+
3+
namespace Cleantalk\Antispam\Integrations;
4+
5+
use Cleantalk\ApbctWP\Variables\Post;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class TestEventPrime extends TestCase
9+
{
10+
private $integration;
11+
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
$_POST = [];
16+
Post::getInstance()->variables = [];
17+
$this->integration = new EventPrime();
18+
}
19+
20+
protected function tearDown(): void
21+
{
22+
$_POST = [];
23+
Post::getInstance()->variables = [];
24+
parent::tearDown();
25+
}
26+
27+
/**
28+
* Test returns null when POST has no EventPrime fields
29+
*/
30+
public function testGetDataForCheckingReturnsNullWithEmptyPost()
31+
{
32+
$result = $this->integration->getDataForChecking(null);
33+
34+
$this->assertNull($result);
35+
}
36+
37+
/**
38+
* Test returns null when POST has unrelated action
39+
*/
40+
public function testGetDataForCheckingReturnsNullWithUnrelatedAction()
41+
{
42+
$_POST['action'] = 'some_other_action';
43+
$_POST['email'] = 'test@example.com';
44+
45+
$result = $this->integration->getDataForChecking(null);
46+
47+
$this->assertNull($result);
48+
}
49+
50+
/**
51+
* Test register form returns array when nonce is present
52+
*/
53+
public function testGetDataForCheckingRegisterFormReturnsArray()
54+
{
55+
$_POST['ep-attendee-register-nonce'] = 'abc123';
56+
$_POST['ep_rg_field_email'] = 'user@example.com';
57+
$_POST['ep_rg_field_first_name'] = 'John';
58+
59+
$result = $this->integration->getDataForChecking(null);
60+
61+
$this->assertIsArray($result);
62+
}
63+
64+
/**
65+
* Test register form sets register flag to true
66+
*/
67+
public function testGetDataForCheckingRegisterFormHasRegisterFlag()
68+
{
69+
$_POST['ep-attendee-register-nonce'] = 'abc123';
70+
$_POST['ep_rg_field_email'] = 'user@example.com';
71+
72+
$result = $this->integration->getDataForChecking(null);
73+
74+
$this->assertIsArray($result);
75+
$this->assertTrue($result['register']);
76+
}
77+
78+
/**
79+
* Test checkout form returns null when ep_rg_field_email is missing
80+
*/
81+
public function testGetDataForCheckingCheckoutFormReturnsNullWithoutEmail()
82+
{
83+
$_POST['action'] = 'ep_save_event_booking';
84+
$_POST['ep_event_id'] = '42';
85+
86+
$result = $this->integration->getDataForChecking(null);
87+
88+
$this->assertNull($result);
89+
}
90+
91+
/**
92+
* Test checkout form returns array when ep_rg_field_email is present
93+
*/
94+
public function testGetDataForCheckingCheckoutFormWithEmail()
95+
{
96+
$_POST['action'] = 'ep_save_event_booking';
97+
$_POST['ep_rg_field_email'] = 'buyer@example.com';
98+
$_POST['ep_event_id'] = '42';
99+
100+
$result = $this->integration->getDataForChecking(null);
101+
102+
$this->assertIsArray($result);
103+
$this->assertEquals('buyer@example.com', $result['email']);
104+
}
105+
106+
/**
107+
* Test checkout form returns the email in result
108+
*/
109+
public function testGetDataForCheckingCheckoutFormResultContainsEmail()
110+
{
111+
$_POST['action'] = 'ep_save_event_booking';
112+
$_POST['ep_rg_field_email'] = 'buyer@example.com';
113+
114+
$result = $this->integration->getDataForChecking(null);
115+
116+
$this->assertIsArray($result);
117+
$this->assertEquals('buyer@example.com', $result['email']);
118+
}
119+
120+
/**
121+
* Test checkout form parses data string and extracts email from it
122+
*/
123+
public function testGetDataForCheckingCheckoutFormParsesDataString()
124+
{
125+
$_POST['action'] = 'ep_save_event_booking';
126+
$_POST['data'] = 'ep_rg_field_email=encoded%40example.com&ep_event_id=5';
127+
128+
$result = $this->integration->getDataForChecking(null);
129+
130+
$this->assertIsArray($result);
131+
$this->assertEquals('encoded@example.com', $result['email']);
132+
}
133+
134+
/**
135+
* Test checkout form data string without email still returns null
136+
*/
137+
public function testGetDataForCheckingCheckoutFormDataStringWithoutEmailReturnsNull()
138+
{
139+
$_POST['action'] = 'ep_save_event_booking';
140+
$_POST['data'] = 'ep_event_id=5&ep_attendee_count=1';
141+
142+
$result = $this->integration->getDataForChecking(null);
143+
144+
$this->assertNull($result);
145+
}
146+
147+
/**
148+
* Test event token is extracted and added to result
149+
*/
150+
public function testGetDataForCheckingExtractsEventToken()
151+
{
152+
$_POST['ep-attendee-register-nonce'] = 'nonce123';
153+
$_POST['ep_rg_field_email'] = 'user@example.com';
154+
$_POST['ct_bot_detector_event_token'] = 'token_abc';
155+
156+
$result = $this->integration->getDataForChecking(null);
157+
158+
$this->assertIsArray($result);
159+
$this->assertEquals('token_abc', $result['event_token']);
160+
}
161+
162+
/**
163+
* Test event token is removed from main input_data fields
164+
*/
165+
public function testGetDataForCheckingEventTokenNotDuplicatedInResult()
166+
{
167+
$_POST['ep-attendee-register-nonce'] = 'nonce123';
168+
$_POST['ep_rg_field_email'] = 'user@example.com';
169+
$_POST['ct_bot_detector_event_token'] = 'token_xyz';
170+
171+
$result = $this->integration->getDataForChecking(null);
172+
173+
$this->assertIsArray($result);
174+
$this->assertEquals('token_xyz', $result['event_token']);
175+
}
176+
177+
/**
178+
* Test no event_token key when token is absent
179+
*/
180+
public function testGetDataForCheckingNoEventTokenKeyWhenAbsent()
181+
{
182+
$_POST['ep-attendee-register-nonce'] = 'nonce123';
183+
$_POST['ep_rg_field_email'] = 'user@example.com';
184+
185+
$result = $this->integration->getDataForChecking(null);
186+
187+
$this->assertIsArray($result);
188+
$this->assertArrayNotHasKey('event_token', $result);
189+
}
190+
191+
/**
192+
* Test checkout form event token extracted from parsed data string
193+
*/
194+
public function testGetDataForCheckingCheckoutFormEventTokenFromDataString()
195+
{
196+
$_POST['action'] = 'ep_save_event_booking';
197+
$_POST['data'] = 'ep_rg_field_email=token%40example.com&ct_bot_detector_event_token=tok123';
198+
199+
$result = $this->integration->getDataForChecking(null);
200+
201+
$this->assertIsArray($result);
202+
$this->assertEquals('tok123', $result['event_token']);
203+
}
204+
205+
/**
206+
* Test register form with special characters in email
207+
*/
208+
public function testGetDataForCheckingRegisterFormWithSpecialCharactersInEmail()
209+
{
210+
$_POST['ep-attendee-register-nonce'] = 'nonce';
211+
$_POST['ep_rg_field_email'] = "special+tag@example.co.uk";
212+
213+
$result = $this->integration->getDataForChecking(null);
214+
215+
$this->assertIsArray($result);
216+
}
217+
218+
/**
219+
* Test that both nonce and checkout action triggers register form logic (nonce takes precedence)
220+
*/
221+
public function testGetDataForCheckingRegisterNonceTakesPrecedenceOverCheckoutAction()
222+
{
223+
$_POST['ep-attendee-register-nonce'] = 'nonce';
224+
$_POST['action'] = 'ep_save_event_booking';
225+
$_POST['ep_rg_field_email'] = 'user@example.com';
226+
227+
$result = $this->integration->getDataForChecking(null);
228+
229+
$this->assertIsArray($result);
230+
$this->assertTrue($result['register']);
231+
}
232+
}

0 commit comments

Comments
 (0)