C:\xampp\htdocs\landing\wp-content\plugins\mailchimp-for-wp\includes\api\class-api.php


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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php

/**
 * Takes care of requests to the Mailchimp API (version 2.0, deprecated)
 *
 * @access public
 * @uses WP_HTTP
 * @since 1.0
 * @deprecated 4.0
 */
class MC4WP_API {


    
/**
     * @var string The URL to the Mailchimp API
     */
    
protected $api_url 'https://api.mailchimp.com/2.0/';

    
/**
     * @var string The API key to use
     */
    
protected $api_key '';

    
/**
     * @var string The error message of the latest API request (if any)
     */
    
protected $error_message '';

    
/**
     * @var int The error code of the last API request (if any)
     */
    
protected $error_code 0;

    
/**
     * @var boolean Boolean indicating whether the user is connected with Mailchimp
     */
    
protected $connected;

    
/**
     * @var object The full response object of the latest API call
     */
    
protected $last_response;

    
/**
     * Constructor
     *
     * @param string $api_key
     */
    
public function __construct$api_key ) {
        
$this->api_key $api_key;

        
$dash_position strpos$api_key'-' );
        if ( 
$dash_position !== false ) {
            
$this->api_url 'https://' substr$api_key$dash_position ) . '.api.mailchimp.com/2.0/';
        }
    }

    
/**
     * Show an error message to administrators
     *
     * @param string $message
     *
     * @return bool
     */
    
private function show_error$message ) {
        if ( ! 
is_admin() || ! current_user_can'manage_options' ) ) {
            return 
false;
        }

        if ( ! 
function_exists'add_settings_error' ) ) {
            return 
false;
        }

        
add_settings_error'mc4wp-api''mc4wp-api-error'$message'error' );
        return 
true;
    }

    
/**
     * @param $message
     *
     * @return bool
     */
    
private function show_connection_error$message ) {
        
$message .= '<br /><br />' sprintf'<a href="%s">' __'Read more about common connectivity issues.''mailchimp-for-wp' ) . '</a>''https://www.mc4wp.com/kb/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice' );
        return 
$this->show_error$message );
    }

    
/**
     * Pings the Mailchimp API to see if we're connected
     *
     * The result is cached to ensure a maximum of 1 API call per page load
     *
     * @return boolean
     */
    
public function is_connected() {
        if ( 
is_bool$this->connected ) ) {
            return 
$this->connected;
        }

        
$result          $this->call'helper/ping' );
        
$this->connected false;

        if ( 
is_object$result ) ) {

            
// Msg key set? All good then!
            
if ( ! empty( $result->msg ) ) {
                
$this->connected true;
                return 
true;
            }

            
// Uh oh. We got an error back.
            
if ( isset( $result->error ) ) {
                
$this->show_error'Mailchimp Error: ' $result->error );
            }
        }

        return 
$this->connected;
    }

    
/**
     * Sends a subscription request to the Mailchimp API
     *
     * @param string $list_id The list id to subscribe to
     * @param string $email The email address to subscribe
     * @param array $merge_vars Array of extra merge variables
     * @param string $email_type The email type to send to this email address. Possible values are `html` and `text`.
     * @param boolean $double_optin Should this email be confirmed via double opt-in?
     * @param boolean $update_existing Update information if this email is already on list?
     * @param boolean $replace_interests Replace interest groupings, only if update_existing is true.
     * @param boolean $send_welcome Send a welcome e-mail, only if double_optin is false.
     *
     * @return boolean|string True if success, 'error' if error
     */
    
public function subscribe$list_id$email, array $merge_vars = array(), $email_type 'html'$double_optin true$update_existing false$replace_interests true$send_welcome false ) {
        
$data = array(
            
'id'                => $list_id,
            
'email'             => array( 'email' => $email ),
            
'merge_vars'        => $merge_vars,
            
'email_type'        => $email_type,
            
'double_optin'      => $double_optin,
            
'update_existing'   => $update_existing,
            
'replace_interests' => $replace_interests,
            
'send_welcome'      => $send_welcome,
        );

        
$response $this->call'lists/subscribe'$data );

        if ( 
is_object$response ) && isset( $response->email ) ) {
            return 
true;
        }

        return 
false;
    }

    
/**
     * Gets the Groupings for a given List
     * @param int $list_id
     * @return array|boolean
     */
    
public function get_list_groupings$list_id ) {
        
$result $this->call'lists/interest-groupings', array( 'id' => $list_id ) );
        if ( 
is_array$result ) ) {
            return 
$result;
        }

        return 
false;
    }

    
/**
     * @param array $list_ids Array of ID's of the lists to fetch. (optional)
     *
     * @return bool
     */
    
public function get_lists$list_ids = array() ) {
        
$args = array(
            
'limit'      => 100,
            
'sort_field' => 'web',
            
'sort_dir'   => 'ASC',
        );

        
// set filter if the $list_ids parameter was set
        
if ( count$list_ids ) > ) {
            
$args['filters'] = array(
                
'list_id' => implode','$list_ids ),
            );
        }

        
$result $this->call'lists/list'$args );

        if ( 
is_object$result ) && isset( $result->data ) ) {
            return 
$result->data;
        }

        return 
false;
    }

    
/**
     * Get the lists an email address is subscribed to
     *
     * @param array|string $email
     *
     * @return array
     */
    
public function get_lists_for_email$email ) {
        if ( 
is_string$email ) ) {
            
$email = array(
                
'email' => $email,
            );
        }

        
$result $this->call'helper/lists-for-email', array( 'email' => $email ) );
        if ( ! 
is_array$result ) ) {
            return array();
        }

        return 
$result;
    }

    
/**
     * Get lists with their merge_vars for a given array of list id's
     * @param array $list_ids
     * @return array|bool
     */
    
public function get_lists_with_merge_vars$list_ids ) {
        
$result $this->call'lists/merge-vars', array( 'id' => $list_ids ) );

        if ( 
is_object$result ) && isset( $result->data ) ) {
            return 
$result->data;
        }

        return 
false;
    }

    
/**
     * Gets the member info for one or multiple emails on a list
     *
     * @param string $list_id
     * @param array $emails
     * @return array|bool
     */
    
public function get_subscriber_info$list_id$emails ) {
        if ( 
is_string$emails ) ) {
            
$emails = array( $emails );
        }

        
$result $this->call(
            
'lists/member-info',
            array(
                
'id'     => $list_id,
                
'emails' => $emails,
            )
        );

        if ( 
is_object$result ) && isset( $result->data ) ) {
            return 
$result->data;
        }

        return 
false;
    }

    
/**
     * Checks if an email address is on a given list
     *
     * @param string $list_id
     * @param string $email
     * @return bool
     */
    
public function list_has_subscriber$list_id$email ) {
        
$member_info $this->get_subscriber_info$list_id, array( array( 'email' => $email ) ) );

        if ( 
is_array$member_info ) && isset( $member_info[0] ) ) {
            return ( 
$member_info[0]->status === 'subscribed' );
        }

        return 
false;
    }

    
/**
     * @param string $list_id
     * @param array|string $email
     * @param array  $merge_vars
     * @param string $email_type
     * @param bool   $replace_interests
     *
     * @return bool
     */
    
public function update_subscriber$list_id$email$merge_vars = array(), $email_type 'html'$replace_interests false ) {

        
// default to using email for updating
        
if ( is_string$email ) ) {
            
$email = array(
                
'email' => $email,
            );
        }

        
$result $this->call(
            
'lists/update-member',
            array(
                
'id'                => $list_id,
                
'email'             => $email,
                
'merge_vars'        => $merge_vars,
                
'email_type'        => $email_type,
                
'replace_interests' => $replace_interests,
            )
        );

        if ( 
is_object$result ) ) {
            if ( isset( 
$result->error ) ) {
                return 
false;
            } else {
                return 
true;
            }
        }

        return 
false;
    }

    
/**
     * Unsubscribes the given email or luid from the given Mailchimp list
     *
     * @param string       $list_id
     * @param array|string $struct
     * @param bool         $delete_member
     * @param bool         $send_goodbye
     * @param bool         $send_notification
     *
     * @return bool
     */
    
public function unsubscribe$list_id$struct$send_goodbye true$send_notification false$delete_member false ) {
        if ( ! 
is_array$struct ) ) {
            
// assume $struct is an email
            
$struct = array(
                
'email' => $struct,
            );
        }

        
$response $this->call(
            
'lists/unsubscribe',
            array(
                
'id'            => $list_id,
                
'email'         => $struct,
                
'delete_member' => $delete_member,
                
'send_goodbye'  => $send_goodbye,
                
'send_notify'   => $send_notification,
            )
        );

        if ( 
is_object$response ) ) {
            if ( isset( 
$response->complete ) && $response->complete ) {
                return 
true;
            }
        }

        return 
false;
    }

    
/**
     * @see https://apidocs.mailchimp.com/api/2.0/ecomm/order-add.php
     *
     * @param array $order_data
     *
     * @return boolean
     */
    
public function add_ecommerce_order( array $order_data ) {
        
$response $this->call'ecomm/order-add', array( 'order' => $order_data ) );

        if ( 
is_object$response ) ) {

            
// complete means success
            
if ( isset( $response->complete ) && $response->complete ) {
                return 
true;
            }

            
// if order was already added: great
            
if ( isset( $response->code$response->error ) && $response->code == 330 && strpos$response->error'already been recorded' ) !== false ) {
                return 
true;
            }
        }

        return 
false;
    }

    
/**
     * @see https://apidocs.mailchimp.com/api/2.0/ecomm/order-del.php
     *
     * @param string $store_id
     * @param string $order_id
     *
     * @return bool
     */
    
public function delete_ecommerce_order$store_id$order_id ) {
        
$data = array(
            
'store_id' => $store_id,
            
'order_id' => $order_id,
        );

        
$response $this->call'ecomm/order-del'$data );

        if ( 
is_object$response ) ) {
            if ( isset( 
$response->complete ) && $response->complete ) {
                return 
true;
            }

            
// Invalid order (order not existing). Good!
            
if ( isset( $response->code ) && $response->code == 330 ) {
                return 
true;
            }
        }

        return 
false;
    }



    
/**
     * Calls the Mailchimp API
     *
     * @uses WP_HTTP
     *
     * @param string $method
     * @param array $data
     *
     * @return object|bool
     */
    
public function call$method, array $data = array() ) {
        
$this->empty_last_response();

        
// do not make request when no api key was provided.
        
if ( empty( $this->api_key ) ) {
            
$this->error_message 'Missing Mailchimp API key.';
            
$this->error_code    001;
            return 
false;
        }

        
// do not make request if helper/ping failed already
        
if ( $this->connected === false ) {
            return 
false;
        }

        
$data['apikey'] = $this->api_key;

        
$url          $this->api_url $method '.json';
        
$request_args = array(
            
'body'      => $data,
            
'timeout'   => 10,
            
'headers'   => $this->get_headers(),
            
'sslverify' => apply_filters'mc4wp_use_sslverify'true ),
        );

        
$response wp_remote_post$url$request_args );

        try {
            
$response $this->parse_response$response );
        } catch ( 
Exception $e ) {
            
$this->error_code    $e->getCode();
            
$this->error_message $e->getMessage();
            
$this->show_connection_error$e->getMessage() );
            return 
false;
        }

        
// store response
        
$this->last_response $response;

        
// store error (if any)
        
if ( is_object$response ) ) {
            if ( ! empty( 
$response->error ) ) {
                
$this->error_message $response->error;
            }

            
// store error code (if any)
            
if ( ! empty( $response->code ) ) {
                
$this->error_code = (int) $response->code;
            }
        }

        return 
$response;
    }

    
/**
     * Checks if an error occured in the most recent request
     * @return boolean
     */
    
public function has_error() {
        return ( ! empty( 
$this->error_message ) );
    }

    
/**
     * Gets the most recent error message
     * @return string
     */
    
public function get_error_message() {
        return 
$this->error_message;
    }

    
/**
     * Gets the most recent error code
     *
     * @return int
     */
    
public function get_error_code() {
        return 
$this->error_code;
    }

    
/**
     * Get the most recent response object
     *
     * @return object
     */
    
public function get_last_response() {
        return 
$this->last_response;
    }

    
/**
     * Empties all data from previous response
     */
    
private function empty_last_response() {
        
$this->last_response null;
        
$this->error_code    0;
        
$this->error_message '';
    }

    
/**
     * Get the request headers to send to the Mailchimp API
     *
     * @return array
     */
    
private function get_headers() {
        global 
$wp_version;

        
$headers = array(
            
'Accept'     => 'application/json',
            
'User-Agent' => 'mc4wp/' MC4WP_VERSION '; WordPress/' $wp_version '; ' get_bloginfo'url' ),
        );

        
// Copy Accept-Language from browser headers
        
if ( ! empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
            
$headers['Accept-Language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        }

        return 
$headers;
    }

    
/**
     * @param array|WP_Error $response
     * @return object
     * @throws Exception
     */
    
private function parse_response$response ) {
        if ( 
is_wp_error$response ) ) {
            throw new 
Exception'Error connecting to Mailchimp. ' $response->get_error_message(), (int) $response->get_error_code() );
        }

        
// decode response body
        
$body wp_remote_retrieve_body$response );
        
$data json_decode$body );
        if ( ! 
is_null$data ) ) {
            return 
$data;
        }

        
$code    = (int) wp_remote_retrieve_response_code$response );
        
$message wp_remote_retrieve_response_message$response );

        if ( 
$code !== 200 ) {
            
$message sprintf'The Mailchimp API server returned the following response: <em>%s %s</em>.'$code$message );

            
// check for Akamai firewall response
            
if ( $code === 403 ) {
                
preg_match'/Reference (.*)/i'$body$matches );

                if ( ! empty( 
$matches[1] ) ) {
                    
$message .= '</strong><br /><br />' sprintf'This usually means that your server is blacklisted by Mailchimp\'s firewall. Please contact Mailchimp support with the following reference number: %s </strong>'$matches[1] );
                }
            }
        }

        throw new 
Exception$message$code );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586