Skip to content

Commit 702b54f

Browse files
committed
Added fluent_crm/before_contact_unsubscribe_from_email
1 parent 0ef0563 commit 702b54f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<explain-block title="fluent_crm/before_contact_unsubscribe_from_email">
2+
This action runs just after a contact unsubscribes by clicking the unsubscribe link in an email or from the email header.
3+
4+
**Parameters**
5+
- `$subscriber` Campaign Model
6+
- `$campaignEmail` CampaignEmail Model or null
7+
- `$scope` string 'from_header' / 'web_ui'
8+
9+
**Usage:**
10+
```php
11+
add_action('fluent_crm/before_contact_unsubscribe_from_email', function($subscriber, $campaignEmail, $scope) {
12+
// Do you staffs here
13+
}, 10, 3);
14+
```
15+
16+
**Example:**
17+
18+
Unsubscribe a user from a specific lists instead of globally depends on the sending lists (If only Lists are being selected when sending)
19+
20+
```php
21+
add_action('fluent_crm/before_contact_unsubscribe_from_email', function($subscriber, $campaignEmail, $scope) {
22+
// Do you staffs here
23+
if(!$campaignEmail || !$campaignEmail->campaign) {
24+
return false; // the campaign email or the campaign is not availble
25+
}
26+
27+
$settings = $campaignEmail->campaign->settings;
28+
29+
$sendingType = \FluentCrm\Framework\Support\Arr::get($settings, 'sending_filter');
30+
31+
if($sendingType != 'list_tag') {
32+
return false; // this campaign is not using list tag
33+
}
34+
35+
$sendingListIds = [];
36+
37+
foreach ($settings['subscribers'] as $segment) {
38+
$sendingListIds[] = \FluentCrm\Framework\Support\Arr::get($segment, 'list', 0);
39+
}
40+
41+
$sendingListIds = array_values(array_filter(array_unique($sendingListIds)));
42+
$sendingListIds = array_map('intval', $sendingListIds);
43+
if(empty($sendingListIds)) {
44+
return false; // no list ids found
45+
}
46+
47+
// We will now, remove the lists from the $subscriber object
48+
$subscriber->detachLists($sendingListIds);
49+
50+
wp_send_json_success([
51+
'message' => 'You are unsubscribed from the lists',
52+
'redirect_url' => ''// provide a redirect url if you want
53+
], 200);
54+
55+
}, 10, 3);
56+
```
57+
58+
</explain-block>

src/hooks/actions/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ add_action('fluent_crm/email_header', function($designName) {
144144

145145
!!!include(./src/hooks/actions/_view_on_browser_page_actions.md)!!!
146146

147+
### Self Unsubscribe Actions
148+
<hr />
149+
!!!include(./src/hooks/actions/_before_contact_unsubscribe_from_email.md)!!!
150+
147151
### Fluent Forms - Contact Specific
148152
<hr />
149153

0 commit comments

Comments
 (0)