|
| 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> |
0 commit comments