1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
<?php
namespace AutomateWoo;
use MailPoet\API\MP\v1\APIException;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class integrating MailPoet API * * @see https://github.com/mailpoet/mailpoet/tree/trunk/doc * @class Integration_Mailpoet * @since 5.6.10 */ class Integration_Mailpoet extends Integration {
/** @var string */ public $integration_id = 'mailpoet';
/** * @var \MailPoet\API\MP\v1\API */ private $api;
private const STATUS_SUBSCRIBED = 'subscribed';
/** * Constructor * * @param \MailPoet\API\MP\v1\API $api The API Object */ public function __construct( $api ) { $this->api = $api; }
/** * The MailPoet Integration doesn't rely on a user provided API keym so we don't need to test it. * * @return bool */ public function test_integration(): bool { return true; }
/** * Check if the integration is enabled. * * @return bool */ public function is_enabled(): bool { return (bool) Integrations::is_mailpoet_active(); }
/** * This functions allows adding hooks for MailPoet * * @since 5.7.0 */ public function init_hooks() { add_action( 'automatewoo/customer/opted_out', [ $this, 'sync_opt_out' ] ); add_action( 'automatewoo/customer/before_is_unsubscribed', [ $this, 'sync_subscribed_status' ] ); }
/** * Get Mailpoet lists. * It filters the list discarding those in trash. * * @return array An Associative array with the list ID as a key and the list name as a value. */ public function get_lists() {
$list_names = [];
$lists = $this->api->getLists();
foreach ( $lists as $list ) { // getLists method includes trashed list. We exclude those from the returned list in AW. if ( is_null( $list['deleted_at'] ) ) { $list_names[ $list['id'] ] = $list['name']; } }
return $list_names; }
/** * Gets a subscriber by email. * When the subscriber doesn't exist it returns null. * * @param string $subscriber_email The subscriber email. * @return array|null The subscriber or null if it doesn't exist. */ private function get_subscriber( $subscriber_email ) { try { return $this->api->getSubscriber( $subscriber_email ); } catch ( \Exception $e ) { return null; } }
/** * Add a subscriber to a list. If the subscriber doesn't exist it will be created. * * @param array $subscriber_data Subscriber data. * @param string $list_id List ID * @param array $options API Call options. Like email confirmation. * @throws APIException When the subscribe action fails. */ public function subscribe( $subscriber_data, $list_id, $options = [] ) { $subscriber = $this->get_subscriber( $subscriber_data['email'] );
if ( is_null( $subscriber ) ) { $this->api->addSubscriber( $subscriber_data, [ $list_id ], $options ); } else { $this->api->subscribeToList( $subscriber['id'], $list_id, $options ); } }
/** * Removes a subscriber from a list. If the subscriber doesn't exist it fails. * * @param string $subscriber_email Subscriber data. * @param string $list_id List ID * @throws \Exception When the subscriber doesn't exist. */ public function unsubscribe( $subscriber_email, $list_id ) { $subscriber = $this->get_subscriber( $subscriber_email );
if ( is_null( $subscriber ) ) { throw new \Exception( "Subscriber doesn't exist" ); }
$this->api->unsubscribeFromList( $subscriber['id'], $list_id ); }
/** * Opt-out a customer when that customer opt-out from AutomateWoo * * @see https://github.com/woocommerce/automatewoo/issues/1313 * @since 5.7.0 * @param Customer $customer The customer object. */ public function sync_opt_out( $customer ) { $subscriber = $this->get_subscriber( $customer->get_email() );
if ( $this->is_subscribed( $subscriber ) ) { try { $this->api->unsubscribe( $customer->get_email() ); } catch ( APIException $e ) { return; } } }
/** * Subscribes an AW user in case the user is subscribed in MailPoet * * @see Customer::is_unsubscribed() * @since 5.7.0 * @param Customer $customer The customer object */ public function sync_subscribed_status( $customer ) {
// If customer is subscribed in AW, no need to verify MailPoet Status. if ( true === $customer->get_is_subscribed() ) { return; }
$subscriber = $this->get_subscriber( $customer->get_email() );
// If customer is subscribed in MailPoet then we opt in the customer in AW. if ( $this->is_subscribed( $subscriber ) ) { $customer->opt_in(); } }
/** * Check if a user is subscribed. * * @param array $subscriber The subscriber data * @return bool True if the user is subscribed. */ private function is_subscribed( $subscriber ) { return ! is_null( $subscriber ) && $subscriber['status'] === self::STATUS_SUBSCRIBED; } }
|