PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14.2
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14.2
7.8.14.2 7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / providers / mailchimp / hustle-mailchimp-api.php
wordpress-popup / inc / providers / mailchimp Last commit date
images 1 week ago views 1 week ago hustle-mailchimp-api.php 1 week ago hustle-mailchimp-form-hooks.php 1 week ago hustle-mailchimp-form-settings.php 1 week ago hustle-mailchimp.php 1 week ago mailchimp.php 1 week ago
hustle-mailchimp-api.php
470 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Hustle_Mailchimp_Api class
4 *
5 * @package Hustle
6 */
7
8 /**
9 * Mailchimp API
10 *
11 * @class Hustle_Mailchimp_Api
12 **/
13 class Hustle_Mailchimp_Api {
14
15 /**
16 * Api key
17 *
18 * @var string
19 */
20 private $api_key;
21 /**
22 * Data center
23 *
24 * @var string
25 */
26 private $data_center;
27 /**
28 * User
29 *
30 * @var string
31 */
32 private $user;
33
34 /**
35 * The <dc> part of the URL corresponds to the data center for your account. For example, if the last part of your Mailchimp API key is us6, all API endpoints for your account are available at https://us6.api.mailchimp.com/3.0/.
36 *
37 * @var string
38 */
39 private $endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
40
41 /**
42 * Constructs class with required data
43 *
44 * Hustle_Mailchimp_Api constructor.
45 *
46 * @param string $api_key Api key.
47 * @param string $data_center Data center.
48 */
49 public function __construct( $api_key, $data_center ) {
50 $this->api_key = $api_key;
51 $this->data_center = $data_center;
52 $this->endpoint = str_replace( '<dc>', $data_center, $this->endpoint );
53 $this->user = wp_get_current_user()->display_name;
54 }
55
56 /**
57 * Sends request to the endpoint url with the provided $action
58 *
59 * @param string $action rest action.
60 * @param string $verb Verb.
61 * @param array $args Args.
62 * @return object|WP_Error
63 */
64 private function request( $action, $verb = 'GET', $args = array() ) {
65 $url = trailingslashit( $this->endpoint ) . $action;
66
67 $_args = array(
68 'method' => $verb,
69 'headers' => array(
70 'Authorization' => 'apikey ' . $this->api_key,
71 'Content-Type' => 'application/json;charset=utf-8',
72 ),
73 );
74
75 if ( 'GET' === $verb ) {
76 $url .= ( '?' . http_build_query( $args ) );
77 } elseif ( ! empty( $args['body'] ) ) {
78 $_args['body'] = wp_json_encode( $args['body'] );
79 }
80
81 $res = wp_remote_request( $url, $_args );
82
83 $utils = Hustle_Provider_Utils::get_instance();
84 $utils->last_url_request = $url;
85 $utils->last_data_received = $res;
86 $utils->last_data_sent = $_args;
87
88 if ( ! is_wp_error( $res ) && is_array( $res ) ) {
89 if ( $res['response']['code'] <= 204 ) {
90 return json_decode( wp_remote_retrieve_body( $res ) );
91 }
92
93 $err = new WP_Error();
94 $err->add( $res['response']['code'], $res['response']['message'], $res['body'] );
95 return $err;
96 }
97
98 return $res;
99 }
100
101 /**
102 * Sends rest GET request
103 *
104 * @param string $action Action.
105 * @param array $args Args.
106 * @return array|mixed|object|WP_Error
107 */
108 private function get( $action, $args = array() ) {
109 return $this->request( $action, 'GET', $args );
110 }
111
112 /**
113 * Sends rest GET request
114 *
115 * @param string $action Action.
116 * @param array $args Args.
117 * @return array|mixed|object|WP_Error
118 */
119 private function delete( $action, $args = array() ) {
120 return $this->request( $action, 'DELETE', $args );
121 }
122
123 /**
124 * Sends rest POST request
125 *
126 * @param string $action Action.
127 * @param array $args Args.
128 * @return array|mixed|object|WP_Error
129 */
130 private function post( $action, $args = array() ) {
131 return $this->request( $action, 'POST', $args );
132 }
133
134 /**
135 * Sends rest PUT request
136 *
137 * @param string $action Action.
138 * @param array $args Args.
139 * @return array|mixed|object|WP_Error
140 */
141 private function put( $action, $args = array() ) {
142 return $this->request( $action, 'PUT', $args );
143 }
144
145 /**
146 * Sends rest PATCH request
147 *
148 * @param string $action Action.
149 * @param array $args Args.
150 * @return array|mixed|object|WP_Error
151 */
152 private function patch( $action, $args = array() ) {
153 return $this->request( $action, 'PATCH', $args );
154 }
155
156 /**
157 * Get User Info for the current API KEY
158 *
159 * @param array $fields Fields.
160 * @return array|mixed|object|WP_Error
161 */
162 public function get_info( $fields = array() ) {
163 if ( empty( $fields ) ) {
164 $fields = array( 'account_id', 'account_name', 'email' );
165 }
166
167 return $this->request(
168 '',
169 'GET',
170 array(
171 'fields' => implode( ',', $fields ),
172 )
173 );
174 }
175
176 /**
177 * Gets all the lists
178 *
179 * @param int $offset - current total lists to show.
180 * @param int $count - current total lists to show.
181 *
182 * @return array|mixed|object|WP_Error
183 */
184 public function get_lists( $offset = 50, $count = 10 ) {
185 return $this->get(
186 'lists',
187 array(
188 'user' => $this->user . ':' . $this->api_key,
189 'offset' => $offset,
190 'count' => $count,
191 )
192 );
193 }
194
195 /**
196 * Gets all the groups under a list
197 *
198 * @param string $list_id List ID.
199 * @param int $total Total.
200 *
201 * @return array|mixed|object|WP_Error
202 */
203 public function get_interest_categories( $list_id, $total = 10 ) {
204 return $this->get(
205 'lists/' . $list_id . '/interest-categories',
206 array(
207 'user' => $this->user . ':' . $this->api_key,
208 'count' => $total,
209 )
210 );
211 }
212
213 /**
214 * Gets all the GDPR fields under a list
215 *
216 * @param string $list_id List ID.
217 *
218 * @return array
219 */
220 public function get_gdpr_fields( $list_id ) {
221 $gdpr_fieds = array();
222 $members = $this->get_members( $list_id );
223 if ( ! $members ) {
224 $email = 'dummy@incsub.com';
225 $args = array(
226 'email_address' => $email,
227 'status' => 'unsubscribed',
228 );
229 $this->subscribe( $list_id, $args );
230 $members = $this->get_members( $list_id );
231 $this->delete_email( $list_id, $email );
232 }
233
234 if ( empty( $members ) || ! is_array( $members ) || empty( $members[0]->marketing_permissions ) || ! is_array( $members[0]->marketing_permissions ) ) {
235 return $gdpr_fieds;
236 }
237
238 foreach ( $members[0]->marketing_permissions as $value ) {
239 if ( ! isset( $value->marketing_permission_id ) || ! isset( $value->text ) ) {
240 continue;
241 }
242 $gdpr_fieds[ $value->marketing_permission_id ] = $value->text;
243 }
244
245 return $gdpr_fieds;
246 }
247
248 /**
249 * Get members by list ID
250 *
251 * @param string $list_id List ID.
252 * @return array
253 */
254 private function get_members( $list_id ) {
255 $data = $this->get(
256 'lists/' . $list_id . '/members',
257 array(
258 'user' => $this->user . ':' . $this->api_key,
259 )
260 );
261
262 return $data && is_object( $data ) && ! empty( $data->members ) ? $data->members : array();
263 }
264
265 /**
266 * Gets all the interests under a group list
267 *
268 * @param string $list_id List ID.
269 * @param string $category_id Category ID.
270 * @param int $total Total.
271 *
272 * @return array|mixed|object|WP_Error
273 */
274 public function get_interests( $list_id, $category_id, $total = 10 ) {
275 return $this->get(
276 'lists/' . $list_id . '/interest-categories/' . $category_id . '/interests',
277 array(
278 'user' => $this->user . ':' . $this->api_key,
279 'count' => $total,
280 )
281 );
282 }
283
284 /**
285 * Gets all the tags/static segments on a list
286 *
287 * @param string $list_id List ID.
288 *
289 * @return array|mixed|object|WP_Error
290 */
291 public function get_tags( $list_id ) {
292 return $this->get(
293 'lists/' . $list_id . '/segments',
294 array(
295 'count' => 1000,
296 'user' => $this->user . ':' . $this->api_key,
297 'type' => 'static',
298 )
299 );
300 }
301
302 /**
303 * Check member email address if already existing
304 *
305 * @param string $list_id List ID.
306 * @param string $email Email.
307 *
308 * @return array|mixed|object|WP_Error
309 */
310 public function check_email( $list_id, $email ) {
311 $md5_email = md5( strtolower( $email ) );
312 return $this->get(
313 'lists/' . $list_id . '/members/' . $md5_email,
314 array(
315 'user' => $this->user . ':' . $this->api_key,
316 )
317 );
318 }
319
320 /**
321 * Delete detail of member
322 *
323 * @param string $list_id List ID.
324 * @param string $email Email.
325 *
326 * @return array|mixed|object|WP_Error
327 */
328 public function delete_email( $list_id, $email ) {
329 $md5_email = md5( strtolower( $email ) );
330 $this->update_subscription_patch( $list_id, $email, array( 'status' => 'unsubscribed' ) );
331 return $this->delete( 'lists/' . $list_id . '/members/' . $md5_email );
332 }
333
334 /**
335 * Add custom field for list
336 *
337 * @param string $list_id List ID.
338 * @param array $field_data Field data.
339 *
340 * @return array|mixed|object|WP_Error
341 */
342 public function add_custom_field( $list_id, $field_data ) {
343 return $this->post(
344 'lists/' . $list_id . '/merge-fields',
345 array(
346 'body' => $field_data,
347 )
348 );
349 }
350
351 /**
352 * Get custom fields for list
353 *
354 * @param string $list_id List ID.
355 * @param int $count Count.
356 * @param int $offset Offset.
357 *
358 * @return array|mixed|object|WP_Error
359 */
360 public function get_custom_fields( $list_id, $count = PHP_INT_MAX, $offset = 0 ) {
361 return $this->get(
362 'lists/' . $list_id . '/merge-fields',
363 array(
364 'user' => $this->user . ':' . $this->api_key,
365 'offset' => $offset,
366 'count' => $count,
367 )
368 );
369 }
370
371 /**
372 * Add new subscriber
373 *
374 * @param string $list_id List ID.
375 * @param array $data Data.
376 * @return array|mixed|object|WP_Error
377 * @throws Exception Something went wrong.
378 */
379 public function subscribe( $list_id, $data ) {
380 $res = $this->post(
381 'lists/' . $list_id . '/members',
382 array(
383 'body' => $data,
384 )
385 );
386
387 if ( ! is_wp_error( $res ) ) {
388 return $res;
389 } else {
390 if ( strpos( $res->get_error_data(), '"Forgotten Email Not Subscribed"' ) ) {
391 $error = __( "This contact was previously removed from this list via Mailchimp dashboard. To rejoin, they'll need to sign up using a Mailchimp native form.", 'hustle' );
392 $error .= ' ' . __( 'Subscriber email: ', 'hustle' ) . $data['email_address'];
393 } else {
394 $error = implode( ', ', $res->get_error_messages() );
395 $error .= __( 'Something went wrong.', 'hustle' );
396 $error_data = $res->get_error_data();
397 if ( ! empty( $error_data ) ) {
398 $error .= ' ' . $error_data;
399 }
400 }
401 throw new Exception( esc_html( $error ) );
402 }
403 }
404
405 /**
406 * Update subscription
407 *
408 * @param string $list_id - the list id.
409 * @param string $email - the email.
410 * @param array $data - data.
411 *
412 * @return array|mixed|object|WP_Error
413 * @throws Exception Something went wrong.
414 */
415 public function update_subscription( $list_id, $email, $data ) {
416 $md5_email = md5( strtolower( $email ) );
417 $res = $this->put(
418 'lists/' . $list_id . '/members/' . $md5_email,
419 array(
420 'body' => $data,
421 )
422 );
423
424 if ( ! is_wp_error( $res ) ) {
425 // returns object on success @since 4.0.2 as we need it for GDPR.
426 return $res;
427 }
428 }
429
430 /**
431 * Update subscription
432 *
433 * @param string $list_id - the list id.
434 * @param string $email - the email.
435 * @param array $data - array.
436 *
437 * @return array|mixed|object|WP_Error
438 * @throws Exception Something went wrong.
439 */
440 public function update_subscription_patch( $list_id, $email, $data ) {
441 $md5_email = md5( strtolower( $email ) );
442 if ( ! empty( $data['tags'] ) && is_array( $data['tags'] ) ) {
443 foreach ( $data['tags'] as $tag_id ) {
444 $res = $this->post(
445 'lists/' . $list_id . '/segments/' . $tag_id . '/members/',
446 array(
447 'body' => array(
448 'email_address' => strtolower( $email ),
449 ),
450 )
451 );
452 }
453 unset( $data['tags'] );
454 }
455 $res = $this->patch(
456 'lists/' . $list_id . '/members/' . $md5_email,
457 array(
458 'body' => $data,
459 )
460 );
461
462 $error = __( "Couldn't update the user", 'hustle' );
463 if ( ! is_wp_error( $res ) ) {
464 return __( 'User updated', 'hustle' );
465 } else {
466 throw new Exception( esc_html( $error ) );
467 }
468 }
469 }
470