MailChimp for WordPress - Version 4.1.6

Version Description

Download this release

Release Info

Developer DvanKooten
Plugin Icon 128x128 MailChimp for WordPress
Version 4.1.6
Comparing to
See all releases

Code changes from version 4.1.5 to 4.1.6

CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
  Changelog
2
  =========
3
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  #### 4.1.5 - June 27, 2017
5
 
6
  **Fixes**
1
  Changelog
2
  =========
3
 
4
+ #### 4.1.6 - July 31, 2017
5
+
6
+ **Fixes**
7
+
8
+ - Method on API class for retrieving campaign data.
9
+
10
+ **Improvements**
11
+
12
+ - Show Akamai reference number when an API request is blocked by MailChimp's firewall.
13
+ - Minor output buffering improvements in form previewer.
14
+
15
+
16
  #### 4.1.5 - June 27, 2017
17
 
18
  **Fixes**
includes/admin/class-admin.php CHANGED
@@ -1,58 +1,58 @@
1
  <?php
2
 
3
  /**
4
- * Class MC4WP_Admin
5
- *
6
- * @ignore
7
- * @access private
8
- */
9
  class MC4WP_Admin {
10
 
11
  /**
12
- * @var string The relative path to the main plugin file from the plugins dir
13
- */
14
  protected $plugin_file;
15
 
16
  /**
17
- * @var MC4WP_MailChimp
18
- */
19
  protected $mailchimp;
20
 
21
  /**
22
- * @var MC4WP_Admin_Messages
23
- */
24
  protected $messages;
25
 
26
  /**
27
- * @var MC4WP_Admin_Ads
28
- */
29
  protected $ads;
30
 
31
- /**
32
- * @var MC4WP_Admin_Tools
33
- */
34
- protected $tools;
35
 
36
  /**
37
- * Constructor
38
- *
39
- * @param MC4WP_Admin_Tools $tools
40
- * @param MC4WP_Admin_Messages $messages
41
- * @param MC4WP_MailChimp $mailchimp
42
- */
43
  public function __construct( MC4WP_Admin_Tools $tools, MC4WP_Admin_Messages $messages, MC4WP_MailChimp $mailchimp ) {
44
- $this->tools = $tools;
45
  $this->mailchimp = $mailchimp;
46
  $this->messages = $messages;
47
  $this->plugin_file = plugin_basename( MC4WP_PLUGIN_FILE );
48
  $this->ads = new MC4WP_Admin_Ads();
49
- $this->review_notice = new MC4WP_Admin_Review_Notice( $tools );
50
  $this->load_translations();
51
  }
52
 
53
  /**
54
- * Registers all hooks
55
- */
56
  public function add_hooks() {
57
 
58
  // Actions used globally throughout WP Admin
@@ -70,14 +70,14 @@ class MC4WP_Admin {
70
 
71
  $this->ads->add_hooks();
72
  $this->messages->add_hooks();
73
- $this->review_notice->add_hooks();
74
  }
75
 
76
  /**
77
- * Initializes various stuff used in WP Admin
78
- *
79
- * - Registers settings
80
- */
81
  public function initialize() {
82
 
83
  // register settings
@@ -92,8 +92,8 @@ class MC4WP_Admin {
92
 
93
 
94
  /**
95
- * Listen for `_mc4wp_action` requests
96
- */
97
  public function listen_for_actions() {
98
 
99
  // listen for any action (if user is authorised)
@@ -104,15 +104,15 @@ class MC4WP_Admin {
104
  $action = (string) $_REQUEST['_mc4wp_action'];
105
 
106
  /**
107
- * Allows you to hook into requests containing `_mc4wp_action` => action name.
108
- *
109
- * The dynamic portion of the hook name, `$action`, refers to the action name.
110
- *
111
- * By the time this hook is fired, the user is already authorized. After processing all the registered hooks,
112
- * the request is redirected back to the referring URL.
113
- *
114
- * @since 3.0
115
- */
116
  do_action( 'mc4wp_admin_' . $action );
117
 
118
  // redirect back to where we came from
@@ -122,8 +122,8 @@ class MC4WP_Admin {
122
  }
123
 
124
  /**
125
- * Register dashboard widgets
126
- */
127
  public function register_dashboard_widgets() {
128
 
129
  if( ! $this->tools->is_user_authorized() ) {
@@ -131,21 +131,21 @@ class MC4WP_Admin {
131
  }
132
 
133
  /**
134
- * Setup dashboard widget, users are authorized by now.
135
- *
136
- * Use this hook to register your own dashboard widgets for users with the required capability.
137
- *
138
- * @since 3.0
139
- * @ignore
140
- */
141
  do_action( 'mc4wp_dashboard_setup' );
142
 
143
  return true;
144
  }
145
 
146
  /**
147
- * Upgrade routine
148
- */
149
  private function init_upgrade_routines() {
150
 
151
  // upgrade routine for upgrade routine....
@@ -157,36 +157,36 @@ class MC4WP_Admin {
157
 
158
  $previous_version = get_option( 'mc4wp_version', 0 );
159
 
160
- // allow setting migration version from URL, to easily re-run previous migrations.
161
- if( isset( $_GET['mc4wp_run_migration'] ) ) {
162
- $previous_version = $_GET['mc4wp_run_migration'];
163
- }
164
 
165
- // Ran upgrade routines before?
166
- if( empty( $previous_version ) ) {
167
- update_option( 'mc4wp_version', MC4WP_VERSION );
168
 
169
- // if we have at least one form, we're going to run upgrade routine for v3 => v4 anyway.
170
- // TODO: Remove this once we hit 4.2.x
171
- $posts = get_posts( array( 'post_type' => 'mc4wp-form', 'numberposts' => 1 ) );
172
- if( empty( $posts ) ) {
173
- return false;
174
- }
175
 
176
- $previous_version = '3.9';
177
- }
178
 
179
- // Rollback'ed?
180
- if( version_compare( $previous_version, MC4WP_VERSION, '>' ) ) {
181
- update_option( 'mc4wp_version', MC4WP_VERSION );
182
- return false;
183
- }
184
 
185
  // This means we're good!
186
  if( version_compare( $previous_version, MC4WP_VERSION ) > -1 ) {
187
  return false;
188
  }
189
-
190
  define( 'MC4WP_DOING_UPGRADE', true );
191
  $upgrade_routines = new MC4WP_Upgrade_Routines( $previous_version, MC4WP_VERSION, dirname( __FILE__ ) . '/migrations' );
192
  $upgrade_routines->run();
@@ -194,8 +194,8 @@ class MC4WP_Admin {
194
  }
195
 
196
  /**
197
- * Renew MailChimp lists cache
198
- */
199
  public function renew_lists_cache() {
200
  // try getting new lists to fill cache again
201
  $lists = $this->mailchimp->fetch_lists();
@@ -206,26 +206,26 @@ class MC4WP_Admin {
206
  }
207
 
208
  /**
209
- * Load the plugin translations
210
- */
211
  private function load_translations() {
212
  // load the plugin text domain
213
  load_plugin_textdomain( 'mailchimp-for-wp', false, dirname( $this->plugin_file ) . '/languages' );
214
  }
215
 
216
  /**
217
- * Customize texts throughout WP Admin
218
- */
219
  public function customize_admin_texts() {
220
  $texts = new MC4WP_Admin_Texts( $this->plugin_file );
221
  $texts->add_hooks();
222
  }
223
 
224
  /**
225
- * Validates the General settings
226
- * @param array $settings
227
- * @return array
228
- */
229
  public function save_general_settings( array $settings ) {
230
 
231
  $current = mc4wp_get_options();
@@ -253,32 +253,32 @@ class MC4WP_Admin {
253
 
254
 
255
  /**
256
- * Runs right before general settings are saved.
257
- *
258
- * @param array $settings The updated settings array
259
- * @param array $current The old settings array
260
- */
261
  do_action( 'mc4wp_save_settings', $settings, $current );
262
 
263
  return $settings;
264
  }
265
 
266
  /**
267
- * Load scripts and stylesheet on MailChimp for WP Admin pages
268
- *
269
- * @return bool
270
  */
271
  public function enqueue_assets() {
272
 
273
  global $wp_scripts;
274
 
275
 
276
- if( ! $this->tools->on_plugin_page() ) {
277
- return false;
278
- }
279
 
280
  $opts = mc4wp_get_options();
281
- $page = $this->tools->get_plugin_page();
282
  $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
283
 
284
  // css
@@ -294,222 +294,228 @@ class MC4WP_Admin {
294
  wp_enqueue_script( array( 'jquery', 'es5-shim', 'mc4wp-admin' ) );
295
 
296
  wp_localize_script( 'mc4wp-admin', 'mc4wp_vars',
297
- array(
298
- 'mailchimp' => array(
299
- 'api_connected' => ! empty( $opts['api_key'] ),
300
- 'lists' => $this->mailchimp->get_cached_lists()
301
- ),
302
- 'countries' => MC4WP_Tools::get_countries(),
303
- 'i18n' => array(
304
- 'pro_only' => __( 'This is a pro-only feature. Please upgrade to the premium version to be able to use it.', 'mailchimp-for-wp' ),
305
- 'renew_mailchimp_lists' => __( 'Renew MailChimp lists', 'mailchimp-for-wp' ),
306
- 'fetching_mailchimp_lists' => __( 'Fetching MailChimp lists', 'mailchimp-for-wp' ),
307
- 'fetching_mailchimp_lists_done' => __( 'Done! MailChimp lists renewed.', 'mailchimp-for-wp' ),
308
- 'fetching_mailchimp_lists_can_take_a_while' => __( 'This can take a while if you have many MailChimp lists.', 'mailchimp-for-wp' ),
309
- 'fetching_mailchimp_lists_error' => __( 'Failed to renew your lists. An error occured.', 'mailchimp-for-wp' ),
310
- )
311
- )
312
- );
313
-
314
- /**
315
- * Hook to enqueue your own custom assets on the MailChimp for WordPress setting pages.
316
- *
317
- * @since 3.0
318
- *
319
- * @param string $suffix
320
- * @param string $page
321
- */
322
- do_action( 'mc4wp_admin_enqueue_assets', $suffix, $page );
323
-
324
- return true;
325
- }
326
-
327
-
328
-
329
- /**
330
- * Register the setting pages and their menu items
331
- */
332
- public function build_menu() {
333
- $required_cap = $this->tools->get_required_capability();
334
-
335
- $menu_items = array(
336
- 'general' => array(
337
- 'title' => __( 'MailChimp API Settings', 'mailchimp-for-wp' ),
338
- 'text' => __( 'MailChimp', 'mailchimp-for-wp' ),
339
- 'slug' => '',
340
- 'callback' => array( $this, 'show_generals_setting_page' ),
341
- 'position' => 0
342
  ),
343
- 'other' => array(
344
- 'title' => __( 'Other Settings', 'mailchimp-for-wp' ),
345
- 'text' => __( 'Other', 'mailchimp-for-wp' ),
346
- 'slug' => 'other',
347
- 'callback' => array( $this, 'show_other_setting_page' ),
348
- 'position' => 90
349
- )
350
- );
351
-
352
- /**
353
- * Filters the menu items to appear under the main menu item.
354
- *
355
- * To add your own item, add an associative array in the following format.
356
- *
357
- * $menu_items[] = array(
358
- * 'title' => 'Page title',
359
- * 'text' => 'Menu text',
360
- * 'slug' => 'Page slug',
361
- * 'callback' => 'my_page_function',
362
- * 'position' => 50
363
- * );
364
- *
365
- * @param array $menu_items
366
- * @since 3.0
367
- */
368
- $menu_items = (array) apply_filters( 'mc4wp_admin_menu_items', $menu_items );
369
-
370
- // add top menu item
371
- add_menu_page( 'MailChimp for WP', 'MailChimp for WP', $required_cap, 'mailchimp-for-wp', array( $this, 'show_generals_setting_page' ), MC4WP_PLUGIN_URL . 'assets/img/icon.png', '99.68491' );
372
-
373
- // sort submenu items by 'position'
374
- uasort( $menu_items, array( $this, 'sort_menu_items_by_position' ) );
375
-
376
- // add sub-menu items
377
- array_walk( $menu_items, array( $this, 'add_menu_item' ) );
378
- }
379
-
380
- /**
381
- * @param array $item
382
- */
383
- public function add_menu_item( array $item ) {
384
-
385
- // generate menu slug
386
- $slug = 'mailchimp-for-wp';
387
- if( ! empty( $item['slug'] ) ) {
388
- $slug .= '-' . $item['slug'];
389
- }
390
-
391
- // provide some defaults
392
- $parent_slug = ! empty( $item['parent_slug']) ? $item['parent_slug'] : 'mailchimp-for-wp';
393
- $capability = ! empty( $item['capability'] ) ? $item['capability'] : $this->tools->get_required_capability();
394
-
395
- // register page
396
- $hook = add_submenu_page( $parent_slug, $item['title'] . ' - MailChimp for WordPress', $item['text'], $capability, $slug, $item['callback'] );
397
-
398
- // register callback for loading this page, if given
399
- if( array_key_exists( 'load_callback', $item ) ) {
400
- add_action( 'load-' . $hook, $item['load_callback'] );
401
- }
402
- }
403
-
404
- /**
405
- * Show the API Settings page
406
- */
407
- public function show_generals_setting_page() {
408
- $opts = mc4wp_get_options();
409
-
410
- $connected = ! empty( $opts['api_key'] );
411
- if( $connected ) {
412
- try {
413
- $connected = $this->get_api()->is_connected();
414
- } catch( MC4WP_API_Connection_Exception $e ) {
415
- $message = sprintf( "<strong>%s</strong><br /> %s", __( "Error connecting to MailChimp:", 'mailchimp-for-wp' ), $e );
416
- $message .= '<br /><br />' . sprintf( '<a href="%s">' . __( 'Here\'s some info on solving common connectivity issues.', 'mailchimp-for-wp' ) . '</a>', 'https://kb.mc4wp.com/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice' );
417
- $this->messages->flash( $message, 'error' );
418
- $connected = false;
419
- } catch( MC4WP_API_Exception $e ) {
420
- $this->messages->flash( sprintf( "<strong>%s</strong><br /> %s", __( "MailChimp returned the following error:", 'mailchimp-for-wp' ), $e ), 'error' );
421
- $connected = false;
422
- }
423
- }
424
-
425
- $lists = $this->mailchimp->get_cached_lists();
426
- $obfuscated_api_key = mc4wp_obfuscate_string( $opts['api_key'] );
427
- require MC4WP_PLUGIN_DIR . 'includes/views/general-settings.php';
428
- }
429
-
430
- /**
431
- * Show the Other Settings page
432
- */
433
- public function show_other_setting_page() {
434
- $opts = mc4wp_get_options();
435
- $log = $this->get_log();
436
- $log_reader = new MC4WP_Debug_Log_Reader( $log->file );
437
- require MC4WP_PLUGIN_DIR . 'includes/views/other-settings.php';
438
- }
439
-
440
- /**
441
- * @param $a
442
- * @param $b
443
- *
444
- * @return int
445
- */
446
- public function sort_menu_items_by_position( $a, $b ) {
447
- $pos_a = isset( $a['position'] ) ? $a['position'] : 80;
448
- $pos_b = isset( $b['position'] ) ? $b['position'] : 90;
449
- return $pos_a < $pos_b ? -1 : 1;
450
- }
451
-
452
- /**
453
- * Empties the log file
454
- */
455
- public function empty_debug_log() {
456
- $log = $this->get_log();
457
- file_put_contents( $log->file, '' );
458
-
459
- $this->messages->flash( __( 'Log successfully emptied.', 'mailchimp-for-wp' ) );
460
- }
461
-
462
- /**
463
- * Shows a notice when API key is not set.
464
- */
465
- public function show_api_key_notice() {
466
-
467
- // don't show if on settings page already
468
- if( $this->tools->on_plugin_page( '' ) ) {
469
- return;
470
- }
471
-
472
- // only show to user with proper permissions
473
- if( ! $this->tools->is_user_authorized() ) {
474
- return;
475
- }
476
-
477
- // don't show if dismissed
478
- if( get_transient( 'mc4wp_api_key_notice_dismissed' ) ) {
479
- return;
480
- }
481
-
482
- // don't show if api key is set already
483
- $options = mc4wp_get_options();
484
- if( ! empty( $options['api_key'] ) ) {
485
- return;
486
  }
487
 
488
- echo '<div class="notice notice-warning mc4wp-is-dismissible">';
489
- echo '<p>' . sprintf( __( 'To get started with MailChimp for WordPress, please <a href="%s">enter your MailChimp API key on the settings page of the plugin</a>.', 'mailchimp-for-wp' ), admin_url( 'admin.php?page=mailchimp-for-wp' ) ) . '</p>';
490
- echo '<form method="post"><input type="hidden" name="_mc4wp_action" value="dismiss_api_key_notice" /><button type="submit" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></form>';
491
- echo '</div>';
492
- }
493
-
494
- /**
495
- * Dismisses the API key notice for 1 week
496
- */
497
- public function dismiss_api_key_notice() {
498
- set_transient( 'mc4wp_api_key_notice_dismissed', 1, 3600 * 24 * 7 );
499
- }
500
-
501
- /**
502
- * @return MC4WP_Debug_Log
503
- */
504
- protected function get_log() {
505
- return mc4wp('log');
506
- }
507
 
508
- /**
509
- * @return MC4WP_API_v3
510
- */
511
- protected function get_api() {
512
- return mc4wp('api');
513
- }
514
 
515
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
  /**
4
+ * Class MC4WP_Admin
5
+ *
6
+ * @ignore
7
+ * @access private
8
+ */
9
  class MC4WP_Admin {
10
 
11
  /**
12
+ * @var string The relative path to the main plugin file from the plugins dir
13
+ */
14
  protected $plugin_file;
15
 
16
  /**
17
+ * @var MC4WP_MailChimp
18
+ */
19
  protected $mailchimp;
20
 
21
  /**
22
+ * @var MC4WP_Admin_Messages
23
+ */
24
  protected $messages;
25
 
26
  /**
27
+ * @var MC4WP_Admin_Ads
28
+ */
29
  protected $ads;
30
 
31
+ /**
32
+ * @var MC4WP_Admin_Tools
33
+ */
34
+ protected $tools;
35
 
36
  /**
37
+ * Constructor
38
+ *
39
+ * @param MC4WP_Admin_Tools $tools
40
+ * @param MC4WP_Admin_Messages $messages
41
+ * @param MC4WP_MailChimp $mailchimp
42
+ */
43
  public function __construct( MC4WP_Admin_Tools $tools, MC4WP_Admin_Messages $messages, MC4WP_MailChimp $mailchimp ) {
44
+ $this->tools = $tools;
45
  $this->mailchimp = $mailchimp;
46
  $this->messages = $messages;
47
  $this->plugin_file = plugin_basename( MC4WP_PLUGIN_FILE );
48
  $this->ads = new MC4WP_Admin_Ads();
49
+ $this->review_notice = new MC4WP_Admin_Review_Notice( $tools );
50
  $this->load_translations();
51
  }
52
 
53
  /**
54
+ * Registers all hooks
55
+ */
56
  public function add_hooks() {
57
 
58
  // Actions used globally throughout WP Admin
70
 
71
  $this->ads->add_hooks();
72
  $this->messages->add_hooks();
73
+ $this->review_notice->add_hooks();
74
  }
75
 
76
  /**
77
+ * Initializes various stuff used in WP Admin
78
+ *
79
+ * - Registers settings
80
+ */
81
  public function initialize() {
82
 
83
  // register settings
92
 
93
 
94
  /**
95
+ * Listen for `_mc4wp_action` requests
96
+ */
97
  public function listen_for_actions() {
98
 
99
  // listen for any action (if user is authorised)
104
  $action = (string) $_REQUEST['_mc4wp_action'];
105
 
106
  /**
107
+ * Allows you to hook into requests containing `_mc4wp_action` => action name.
108
+ *
109
+ * The dynamic portion of the hook name, `$action`, refers to the action name.
110
+ *
111
+ * By the time this hook is fired, the user is already authorized. After processing all the registered hooks,
112
+ * the request is redirected back to the referring URL.
113
+ *
114
+ * @since 3.0
115
+ */
116
  do_action( 'mc4wp_admin_' . $action );
117
 
118
  // redirect back to where we came from
122
  }
123
 
124
  /**
125
+ * Register dashboard widgets
126
+ */
127
  public function register_dashboard_widgets() {
128
 
129
  if( ! $this->tools->is_user_authorized() ) {
131
  }
132
 
133
  /**
134
+ * Setup dashboard widget, users are authorized by now.
135
+ *
136
+ * Use this hook to register your own dashboard widgets for users with the required capability.
137
+ *
138
+ * @since 3.0
139
+ * @ignore
140
+ */
141
  do_action( 'mc4wp_dashboard_setup' );
142
 
143
  return true;
144
  }
145
 
146
  /**
147
+ * Upgrade routine
148
+ */
149
  private function init_upgrade_routines() {
150
 
151
  // upgrade routine for upgrade routine....
157
 
158
  $previous_version = get_option( 'mc4wp_version', 0 );
159
 
160
+ // allow setting migration version from URL, to easily re-run previous migrations.
161
+ if( isset( $_GET['mc4wp_run_migration'] ) ) {
162
+ $previous_version = $_GET['mc4wp_run_migration'];
163
+ }
164
 
165
+ // Ran upgrade routines before?
166
+ if( empty( $previous_version ) ) {
167
+ update_option( 'mc4wp_version', MC4WP_VERSION );
168
 
169
+ // if we have at least one form, we're going to run upgrade routine for v3 => v4 anyway.
170
+ // TODO: Remove this once we hit 4.2.x
171
+ $posts = get_posts( array( 'post_type' => 'mc4wp-form', 'numberposts' => 1 ) );
172
+ if( empty( $posts ) ) {
173
+ return false;
174
+ }
175
 
176
+ $previous_version = '3.9';
177
+ }
178
 
179
+ // Rollback'ed?
180
+ if( version_compare( $previous_version, MC4WP_VERSION, '>' ) ) {
181
+ update_option( 'mc4wp_version', MC4WP_VERSION );
182
+ return false;
183
+ }
184
 
185
  // This means we're good!
186
  if( version_compare( $previous_version, MC4WP_VERSION ) > -1 ) {
187
  return false;
188
  }
189
+
190
  define( 'MC4WP_DOING_UPGRADE', true );
191
  $upgrade_routines = new MC4WP_Upgrade_Routines( $previous_version, MC4WP_VERSION, dirname( __FILE__ ) . '/migrations' );
192
  $upgrade_routines->run();
194
  }
195
 
196
  /**
197
+ * Renew MailChimp lists cache
198
+ */
199
  public function renew_lists_cache() {
200
  // try getting new lists to fill cache again
201
  $lists = $this->mailchimp->fetch_lists();
206
  }
207
 
208
  /**
209
+ * Load the plugin translations
210
+ */
211
  private function load_translations() {
212
  // load the plugin text domain
213
  load_plugin_textdomain( 'mailchimp-for-wp', false, dirname( $this->plugin_file ) . '/languages' );
214
  }
215
 
216
  /**
217
+ * Customize texts throughout WP Admin
218
+ */
219
  public function customize_admin_texts() {
220
  $texts = new MC4WP_Admin_Texts( $this->plugin_file );
221
  $texts->add_hooks();
222
  }
223
 
224
  /**
225
+ * Validates the General settings
226
+ * @param array $settings
227
+ * @return array
228
+ */
229
  public function save_general_settings( array $settings ) {
230
 
231
  $current = mc4wp_get_options();
253
 
254
 
255
  /**
256
+ * Runs right before general settings are saved.
257
+ *
258
+ * @param array $settings The updated settings array
259
+ * @param array $current The old settings array
260
+ */
261
  do_action( 'mc4wp_save_settings', $settings, $current );
262
 
263
  return $settings;
264
  }
265
 
266
  /**
267
+ * Load scripts and stylesheet on MailChimp for WP Admin pages
268
+ *
269
+ * @return bool
270
  */
271
  public function enqueue_assets() {
272
 
273
  global $wp_scripts;
274
 
275
 
276
+ if( ! $this->tools->on_plugin_page() ) {
277
+ return false;
278
+ }
279
 
280
  $opts = mc4wp_get_options();
281
+ $page = $this->tools->get_plugin_page();
282
  $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
283
 
284
  // css
294
  wp_enqueue_script( array( 'jquery', 'es5-shim', 'mc4wp-admin' ) );
295
 
296
  wp_localize_script( 'mc4wp-admin', 'mc4wp_vars',
297
+ array(
298
+ 'mailchimp' => array(
299
+ 'api_connected' => ! empty( $opts['api_key'] ),
300
+ 'lists' => $this->mailchimp->get_cached_lists()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  ),
302
+ 'countries' => MC4WP_Tools::get_countries(),
303
+ 'i18n' => array(
304
+ 'pro_only' => __( 'This is a pro-only feature. Please upgrade to the premium version to be able to use it.', 'mailchimp-for-wp' ),
305
+ 'renew_mailchimp_lists' => __( 'Renew MailChimp lists', 'mailchimp-for-wp' ),
306
+ 'fetching_mailchimp_lists' => __( 'Fetching MailChimp lists', 'mailchimp-for-wp' ),
307
+ 'fetching_mailchimp_lists_done' => __( 'Done! MailChimp lists renewed.', 'mailchimp-for-wp' ),
308
+ 'fetching_mailchimp_lists_can_take_a_while' => __( 'This can take a while if you have many MailChimp lists.', 'mailchimp-for-wp' ),
309
+ 'fetching_mailchimp_lists_error' => __( 'Failed to renew your lists. An error occured.', 'mailchimp-for-wp' ),
310
+ )
311
+ )
312
+ );
313
+
314
+ /**
315
+ * Hook to enqueue your own custom assets on the MailChimp for WordPress setting pages.
316
+ *
317
+ * @since 3.0
318
+ *
319
+ * @param string $suffix
320
+ * @param string $page
321
+ */
322
+ do_action( 'mc4wp_admin_enqueue_assets', $suffix, $page );
323
+
324
+ return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  }
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
 
 
 
 
 
 
328
 
329
+ /**
330
+ * Register the setting pages and their menu items
331
+ */
332
+ public function build_menu() {
333
+ $required_cap = $this->tools->get_required_capability();
334
+
335
+ $menu_items = array(
336
+ 'general' => array(
337
+ 'title' => __( 'MailChimp API Settings', 'mailchimp-for-wp' ),
338
+ 'text' => __( 'MailChimp', 'mailchimp-for-wp' ),
339
+ 'slug' => '',
340
+ 'callback' => array( $this, 'show_generals_setting_page' ),
341
+ 'position' => 0
342
+ ),
343
+ 'other' => array(
344
+ 'title' => __( 'Other Settings', 'mailchimp-for-wp' ),
345
+ 'text' => __( 'Other', 'mailchimp-for-wp' ),
346
+ 'slug' => 'other',
347
+ 'callback' => array( $this, 'show_other_setting_page' ),
348
+ 'position' => 90
349
+ )
350
+ );
351
+
352
+ /**
353
+ * Filters the menu items to appear under the main menu item.
354
+ *
355
+ * To add your own item, add an associative array in the following format.
356
+ *
357
+ * $menu_items[] = array(
358
+ * 'title' => 'Page title',
359
+ * 'text' => 'Menu text',
360
+ * 'slug' => 'Page slug',
361
+ * 'callback' => 'my_page_function',
362
+ * 'position' => 50
363
+ * );
364
+ *
365
+ * @param array $menu_items
366
+ * @since 3.0
367
+ */
368
+ $menu_items = (array) apply_filters( 'mc4wp_admin_menu_items', $menu_items );
369
+
370
+ // add top menu item
371
+ add_menu_page( 'MailChimp for WP', 'MailChimp for WP', $required_cap, 'mailchimp-for-wp', array( $this, 'show_generals_setting_page' ), MC4WP_PLUGIN_URL . 'assets/img/icon.png', '99.68491' );
372
+
373
+ // sort submenu items by 'position'
374
+ uasort( $menu_items, array( $this, 'sort_menu_items_by_position' ) );
375
+
376
+ // add sub-menu items
377
+ array_walk( $menu_items, array( $this, 'add_menu_item' ) );
378
+ }
379
+
380
+ /**
381
+ * @param array $item
382
+ */
383
+ public function add_menu_item( array $item ) {
384
+
385
+ // generate menu slug
386
+ $slug = 'mailchimp-for-wp';
387
+ if( ! empty( $item['slug'] ) ) {
388
+ $slug .= '-' . $item['slug'];
389
+ }
390
+
391
+ // provide some defaults
392
+ $parent_slug = ! empty( $item['parent_slug']) ? $item['parent_slug'] : 'mailchimp-for-wp';
393
+ $capability = ! empty( $item['capability'] ) ? $item['capability'] : $this->tools->get_required_capability();
394
+
395
+ // register page
396
+ $hook = add_submenu_page( $parent_slug, $item['title'] . ' - MailChimp for WordPress', $item['text'], $capability, $slug, $item['callback'] );
397
+
398
+ // register callback for loading this page, if given
399
+ if( array_key_exists( 'load_callback', $item ) ) {
400
+ add_action( 'load-' . $hook, $item['load_callback'] );
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Show the API Settings page
406
+ */
407
+ public function show_generals_setting_page() {
408
+ $opts = mc4wp_get_options();
409
+
410
+ $connected = ! empty( $opts['api_key'] );
411
+ if( $connected ) {
412
+ try {
413
+ $connected = $this->get_api()->is_connected();
414
+ } catch( MC4WP_API_Connection_Exception $e ) {
415
+ $message = sprintf( "<strong>%s</strong> %s %s ", __( "Error connecting to MailChimp:", 'mailchimp-for-wp' ), $e->getCode(), $e->getMessage() );
416
+
417
+ if( is_object( $e->data ) && ! empty( $e->data->ref_no ) ) {
418
+ $message .= '<br />' . sprintf( __( 'Looks like your server is blocked by MailChimp\'s firewall. Please contact MailChimp support and include the following reference number: %s', 'mailchimp-for-wp' ), $e->data->ref_no );
419
+ }
420
+
421
+ $message .= '<br /><br />' . sprintf( '<a href="%s">' . __( 'Here\'s some info on solving common connectivity issues.', 'mailchimp-for-wp' ) . '</a>', 'https://kb.mc4wp.com/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice' );
422
+
423
+ $this->messages->flash( $message, 'error' );
424
+ $connected = false;
425
+ } catch( MC4WP_API_Exception $e ) {
426
+ $this->messages->flash( sprintf( "<strong>%s</strong><br /> %s", __( "MailChimp returned the following error:", 'mailchimp-for-wp' ), $e ), 'error' );
427
+ $connected = false;
428
+ }
429
+ }
430
+
431
+ $lists = $this->mailchimp->get_cached_lists();
432
+ $obfuscated_api_key = mc4wp_obfuscate_string( $opts['api_key'] );
433
+ require MC4WP_PLUGIN_DIR . 'includes/views/general-settings.php';
434
+ }
435
+
436
+ /**
437
+ * Show the Other Settings page
438
+ */
439
+ public function show_other_setting_page() {
440
+ $opts = mc4wp_get_options();
441
+ $log = $this->get_log();
442
+ $log_reader = new MC4WP_Debug_Log_Reader( $log->file );
443
+ require MC4WP_PLUGIN_DIR . 'includes/views/other-settings.php';
444
+ }
445
+
446
+ /**
447
+ * @param $a
448
+ * @param $b
449
+ *
450
+ * @return int
451
+ */
452
+ public function sort_menu_items_by_position( $a, $b ) {
453
+ $pos_a = isset( $a['position'] ) ? $a['position'] : 80;
454
+ $pos_b = isset( $b['position'] ) ? $b['position'] : 90;
455
+ return $pos_a < $pos_b ? -1 : 1;
456
+ }
457
+
458
+ /**
459
+ * Empties the log file
460
+ */
461
+ public function empty_debug_log() {
462
+ $log = $this->get_log();
463
+ file_put_contents( $log->file, '' );
464
+
465
+ $this->messages->flash( __( 'Log successfully emptied.', 'mailchimp-for-wp' ) );
466
+ }
467
+
468
+ /**
469
+ * Shows a notice when API key is not set.
470
+ */
471
+ public function show_api_key_notice() {
472
+
473
+ // don't show if on settings page already
474
+ if( $this->tools->on_plugin_page( '' ) ) {
475
+ return;
476
+ }
477
+
478
+ // only show to user with proper permissions
479
+ if( ! $this->tools->is_user_authorized() ) {
480
+ return;
481
+ }
482
+
483
+ // don't show if dismissed
484
+ if( get_transient( 'mc4wp_api_key_notice_dismissed' ) ) {
485
+ return;
486
+ }
487
+
488
+ // don't show if api key is set already
489
+ $options = mc4wp_get_options();
490
+ if( ! empty( $options['api_key'] ) ) {
491
+ return;
492
+ }
493
+
494
+ echo '<div class="notice notice-warning mc4wp-is-dismissible">';
495
+ echo '<p>' . sprintf( __( 'To get started with MailChimp for WordPress, please <a href="%s">enter your MailChimp API key on the settings page of the plugin</a>.', 'mailchimp-for-wp' ), admin_url( 'admin.php?page=mailchimp-for-wp' ) ) . '</p>';
496
+ echo '<form method="post"><input type="hidden" name="_mc4wp_action" value="dismiss_api_key_notice" /><button type="submit" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></form>';
497
+ echo '</div>';
498
+ }
499
+
500
+ /**
501
+ * Dismisses the API key notice for 1 week
502
+ */
503
+ public function dismiss_api_key_notice() {
504
+ set_transient( 'mc4wp_api_key_notice_dismissed', 1, 3600 * 24 * 7 );
505
+ }
506
+
507
+ /**
508
+ * @return MC4WP_Debug_Log
509
+ */
510
+ protected function get_log() {
511
+ return mc4wp('log');
512
+ }
513
+
514
+ /**
515
+ * @return MC4WP_API_v3
516
+ */
517
+ protected function get_api() {
518
+ return mc4wp('api');
519
+ }
520
+
521
+ }
includes/api/class-api-v3-client.php CHANGED
@@ -2,212 +2,217 @@
2
 
3
  class MC4WP_API_v3_Client {
4
 
5
- /**
6
- * @var string
7
- */
8
- private $api_key;
9
-
10
- /**
11
- * @var string
12
- */
13
- private $api_url = 'https://api.mailchimp.com/3.0/';
14
-
15
- /**
16
- * @var array
17
- */
18
- private $last_response;
19
-
20
- /**
21
- * Constructor
22
- *
23
- * @param string $api_key
24
- */
25
- public function __construct( $api_key ) {
26
- $this->api_key = $api_key;
27
-
28
- $dash_position = strpos( $api_key, '-' );
29
- if( $dash_position !== false ) {
30
- $this->api_url = str_replace( '//api.', '//' . substr( $api_key, $dash_position + 1 ) . ".api.", $this->api_url );
31
- }
32
  }
33
-
34
-
35
- /**
36
- * @param string $resource
37
- * @param array $args
38
- *
39
- * @return mixed
40
- */
41
- public function get( $resource, array $args = array() ) {
42
- return $this->request( 'GET', $resource, $args );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  }
44
 
45
- /**
46
- * @param string $resource
47
- * @param array $data
48
- *
49
- * @return mixed
50
- */
51
- public function post( $resource, array $data ) {
52
- return $this->request( 'POST', $resource, $data );
 
 
 
 
 
53
  }
54
 
55
- /**
56
- * @param string $resource
57
- * @param array $data
58
- * @return mixed
59
- */
60
- public function put( $resource, array $data ) {
61
- return $this->request( 'PUT', $resource, $data );
62
- }
63
 
64
- /**
65
- * @param string $resource
66
- * @param array $data
67
- * @return mixed
68
- */
69
- public function patch( $resource, array $data ) {
70
- return $this->request( 'PATCH', $resource, $data );
71
- }
72
 
73
- /**
74
- * @param string $resource
75
- * @return mixed
76
- */
77
- public function delete( $resource ) {
78
- return $this->request( 'DELETE', $resource );
79
- }
80
 
81
- /**
82
- * @param string $method
83
- * @param string $resource
84
- * @param array $data
85
- *
86
- * @return mixed
87
- *
88
- * @throws MC4WP_API_Exception
89
- */
90
- private function request( $method, $resource, array $data = array() ) {
91
- $this->reset();
92
-
93
- // don't bother if no API key was given.
94
- if( empty( $this->api_key ) ) {
95
- throw new MC4WP_API_Exception( "Missing API key", 001 );
96
- }
97
-
98
- $url = $this->api_url . ltrim( $resource, '/' );
99
- $args = array(
100
- 'method' => $method,
101
- 'headers' => $this->get_headers(),
102
- 'timeout' => 10,
103
- 'sslverify' => apply_filters( 'mc4wp_use_sslverify', true ),
104
- );
105
-
106
- // attach arguments (in body or URL)
107
- if( $method === 'GET' ) {
108
- $url = add_query_arg( $data, $url );
109
- } else {
110
- $args['body'] = json_encode( $data );
111
- }
112
-
113
- // perform request
114
- $response = wp_remote_request( $url, $args );
115
- $this->last_response = $response;
116
-
117
- // parse response
118
- $data = $this->parse_response( $response );
119
-
120
- return $data;
121
- }
122
 
123
- /**
124
- * @return array
125
- */
126
- private function get_headers() {
127
- global $wp_version;
128
 
129
- $headers = array();
130
- $headers['Authorization'] = 'Basic ' . base64_encode( 'mc4wp:' . $this->api_key );
131
- $headers['Accept'] = 'application/json';
132
- $headers['Content-Type'] = 'application/json';
133
- $headers['User-Agent'] = 'mc4wp/' . MC4WP_VERSION . '; WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' );
134
 
135
- // Copy Accept-Language from browser headers
136
- if( ! empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
137
- $headers['Accept-Language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
138
- }
139
 
140
- return $headers;
141
- }
 
 
 
 
 
 
142
 
143
- /**
144
- * @param array|WP_Error $response
145
- *
146
- * @return mixed
147
- *
148
- * @throws MC4WP_API_Exception
149
- */
150
- private function parse_response( $response ) {
151
-
152
- if( $response instanceof WP_Error ) {
153
- throw new MC4WP_API_Connection_Exception( $response->get_error_message(), (int) $response->get_error_code() );
154
- }
155
-
156
- // decode response body
157
- $code = (int) wp_remote_retrieve_response_code( $response );
158
- $message = wp_remote_retrieve_response_message( $response );
159
- $body = wp_remote_retrieve_body( $response );
160
-
161
- // set body to "true" in case MailChimp returned No Content
162
- if( $code < 300 && empty( $body ) ) {
163
- $body = "true";
164
- }
165
-
166
- $data = json_decode( $body );
167
-
168
- if( $code >= 400 ) {
169
- if( $code === 404 ) {
170
- throw new MC4WP_API_Resource_Not_Found_Exception( $message, $code, $response, $data );
171
- }
172
-
173
- throw new MC4WP_API_Exception( $message, $code, $response, $data );
174
- }
175
-
176
- if( ! is_null( $data ) ) {
177
- return $data;
178
- }
179
-
180
- // unable to decode response
181
- throw new MC4WP_API_Exception( $message, $code, $response );
182
  }
183
 
184
- /**
185
- * Empties all data from previous response
186
- */
187
- private function reset() {
188
- $this->last_response = null;
189
- }
190
 
191
- /**
192
- * @return string
193
- */
194
- public function get_last_response_body() {
195
- return wp_remote_retrieve_body( $this->last_response );
196
  }
197
 
198
- /**
199
- * @return array
200
- */
201
- public function get_last_response_headers() {
202
- return wp_remote_retrieve_headers( $this->last_response );
203
- }
 
 
 
 
 
204
 
205
- /**
206
- * @return array|WP_Error
207
- */
208
- public function get_last_response() {
209
- return $this->last_response;
210
  }
211
 
 
 
 
212
 
213
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  class MC4WP_API_v3_Client {
4
 
5
+ /**
6
+ * @var string
7
+ */
8
+ private $api_key;
9
+
10
+ /**
11
+ * @var string
12
+ */
13
+ private $api_url = 'https://api.mailchimp.com/3.0/';
14
+
15
+ /**
16
+ * @var array
17
+ */
18
+ private $last_response;
19
+
20
+ /**
21
+ * Constructor
22
+ *
23
+ * @param string $api_key
24
+ */
25
+ public function __construct( $api_key ) {
26
+ $this->api_key = $api_key;
27
+
28
+ $dash_position = strpos( $api_key, '-' );
29
+ if( $dash_position !== false ) {
30
+ $this->api_url = str_replace( '//api.', '//' . substr( $api_key, $dash_position + 1 ) . ".api.", $this->api_url );
 
31
  }
32
+ }
33
+
34
+
35
+ /**
36
+ * @param string $resource
37
+ * @param array $args
38
+ *
39
+ * @return mixed
40
+ */
41
+ public function get( $resource, array $args = array() ) {
42
+ return $this->request( 'GET', $resource, $args );
43
+ }
44
+
45
+ /**
46
+ * @param string $resource
47
+ * @param array $data
48
+ *
49
+ * @return mixed
50
+ */
51
+ public function post( $resource, array $data ) {
52
+ return $this->request( 'POST', $resource, $data );
53
+ }
54
+
55
+ /**
56
+ * @param string $resource
57
+ * @param array $data
58
+ * @return mixed
59
+ */
60
+ public function put( $resource, array $data ) {
61
+ return $this->request( 'PUT', $resource, $data );
62
+ }
63
+
64
+ /**
65
+ * @param string $resource
66
+ * @param array $data
67
+ * @return mixed
68
+ */
69
+ public function patch( $resource, array $data ) {
70
+ return $this->request( 'PATCH', $resource, $data );
71
+ }
72
+
73
+ /**
74
+ * @param string $resource
75
+ * @return mixed
76
+ */
77
+ public function delete( $resource ) {
78
+ return $this->request( 'DELETE', $resource );
79
+ }
80
+
81
+ /**
82
+ * @param string $method
83
+ * @param string $resource
84
+ * @param array $data
85
+ *
86
+ * @return mixed
87
+ *
88
+ * @throws MC4WP_API_Exception
89
+ */
90
+ private function request( $method, $resource, array $data = array() ) {
91
+ $this->reset();
92
+
93
+ // don't bother if no API key was given.
94
+ if( empty( $this->api_key ) ) {
95
+ throw new MC4WP_API_Exception( "Missing API key", 001 );
96
  }
97
 
98
+ $url = $this->api_url . ltrim( $resource, '/' );
99
+ $args = array(
100
+ 'method' => $method,
101
+ 'headers' => $this->get_headers(),
102
+ 'timeout' => 10,
103
+ 'sslverify' => apply_filters( 'mc4wp_use_sslverify', true ),
104
+ );
105
+
106
+ // attach arguments (in body or URL)
107
+ if( $method === 'GET' ) {
108
+ $url = add_query_arg( $data, $url );
109
+ } else {
110
+ $args['body'] = json_encode( $data );
111
  }
112
 
113
+ // perform request
114
+ $response = wp_remote_request( $url, $args );
115
+ $this->last_response = $response;
 
 
 
 
 
116
 
117
+ // parse response
118
+ $data = $this->parse_response( $response );
 
 
 
 
 
 
119
 
120
+ return $data;
121
+ }
 
 
 
 
 
122
 
123
+ /**
124
+ * @return array
125
+ */
126
+ private function get_headers() {
127
+ global $wp_version;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
+ $headers = array();
130
+ $headers['Authorization'] = 'Basic ' . base64_encode( 'mc4wp:' . $this->api_key );
131
+ $headers['Accept'] = 'application/json';
132
+ $headers['Content-Type'] = 'application/json';
133
+ $headers['User-Agent'] = 'mc4wp/' . MC4WP_VERSION . '; WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' );
134
 
135
+ // Copy Accept-Language from browser headers
136
+ if( ! empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
137
+ $headers['Accept-Language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
138
+ }
 
139
 
140
+ return $headers;
141
+ }
 
 
142
 
143
+ /**
144
+ * @param array|WP_Error $response
145
+ *
146
+ * @return mixed
147
+ *
148
+ * @throws MC4WP_API_Exception
149
+ */
150
+ private function parse_response( $response ) {
151
 
152
+ if( $response instanceof WP_Error ) {
153
+ throw new MC4WP_API_Connection_Exception( $response->get_error_message(), (int) $response->get_error_code() );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  }
155
 
156
+ // decode response body
157
+ $code = (int) wp_remote_retrieve_response_code( $response );
158
+ $message = wp_remote_retrieve_response_message( $response );
159
+ $body = wp_remote_retrieve_body( $response );
 
 
160
 
161
+ // set body to "true" in case MailChimp returned No Content
162
+ if( $code < 300 && empty( $body ) ) {
163
+ $body = "true";
 
 
164
  }
165
 
166
+ $data = json_decode( $body );
167
+ if( $code >= 400 ) {
168
+ // check for akamai errors
169
+ // {"type":"akamai_error_message","title":"akamai_503","status":503,"ref_no":"Reference Number: 00.950e16c3.1498559813.1450dbe2"}
170
+ if( is_object( $data ) && isset( $data->type ) && $data->type === 'akamai_error_message' ) {
171
+ throw new MC4WP_API_Connection_Exception( $message, $code, $response, $data );
172
+ }
173
+
174
+ if( $code === 404 ) {
175
+ throw new MC4WP_API_Resource_Not_Found_Exception( $message, $code, $response, $data );
176
+ }
177
 
178
+ throw new MC4WP_API_Exception( $message, $code, $response, $data );
 
 
 
 
179
  }
180
 
181
+ if( ! is_null( $data ) ) {
182
+ return $data;
183
+ }
184
 
185
+ // unable to decode response
186
+ throw new MC4WP_API_Exception( $message, $code, $response );
187
+ }
188
+
189
+ /**
190
+ * Empties all data from previous response
191
+ */
192
+ private function reset() {
193
+ $this->last_response = null;
194
+ }
195
+
196
+ /**
197
+ * @return string
198
+ */
199
+ public function get_last_response_body() {
200
+ return wp_remote_retrieve_body( $this->last_response );
201
+ }
202
+
203
+ /**
204
+ * @return array
205
+ */
206
+ public function get_last_response_headers() {
207
+ return wp_remote_retrieve_headers( $this->last_response );
208
+ }
209
+
210
+ /**
211
+ * @return array|WP_Error
212
+ */
213
+ public function get_last_response() {
214
+ return $this->last_response;
215
+ }
216
+
217
+
218
+ }
includes/api/class-api-v3.php CHANGED
@@ -830,7 +830,7 @@ class MC4WP_API_v3 {
830
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/templates/#read-get_templates_template_id
831
  * @param string $template_id
832
  * @return object
833
- */
834
  public function get_template( $template_id, array $args = array() ) {
835
  $resource = sprintf( '/templates/%s', $template_id );
836
  return $this->client->get( $resource, $args );
@@ -840,7 +840,7 @@ class MC4WP_API_v3 {
840
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/templates/default-content/
841
  * @param string $template_id
842
  * @return object
843
- */
844
  public function get_template_default_content( $template_id, array $args = array() ) {
845
  $resource = sprintf( '/templates/%s/default-content', $template_id );
846
  return $this->client->get( $resource, $args );
@@ -852,7 +852,7 @@ class MC4WP_API_v3 {
852
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#create-post_campaigns
853
  * @param array $args
854
  * @return object
855
- */
856
  public function add_campaign( array $args ) {
857
  $resource = '/campaigns';
858
  return $this->client->post( $resource, $args );
@@ -864,7 +864,7 @@ class MC4WP_API_v3 {
864
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#read-get_campaigns
865
  * @param array $args
866
  * @return object
867
- */
868
  public function get_campaigns( array $args = array() ) {
869
  $resource = '/campaigns';
870
  return $this->client->get( $resource, $args );
@@ -877,10 +877,10 @@ class MC4WP_API_v3 {
877
  * @param string $campaign_id
878
  * @param array $args
879
  * @return object
880
- */
881
  public function get_campaign( $campaign_id, array $args = array() ) {
882
  $resource = sprintf( '/campaigns/%s', $campaign_id );
883
- return $this->get( $resource, $args );
884
  }
885
 
886
  /**
@@ -890,7 +890,7 @@ class MC4WP_API_v3 {
890
  * @param string $campaign_id
891
  * @param array $args
892
  * @return object
893
- */
894
  public function update_campaign( $campaign_id, array $args ) {
895
  $resource = sprintf( '/campaigns/%s', $campaign_id );
896
  return $this->client->patch( $resource, $args );
@@ -902,7 +902,7 @@ class MC4WP_API_v3 {
902
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#delete-delete_campaigns_campaign_id
903
  * @param string $campaign_id
904
  * @return bool
905
- */
906
  public function delete_campaign( $campaign_id ) {
907
  $resource = sprintf( '/campaigns/%s', $campaign_id );
908
  return !! $this->client->delete( $resource );
@@ -914,10 +914,10 @@ class MC4WP_API_v3 {
914
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns
915
  *
916
  * @param string $campaign_id
917
- * @param string $action
918
  * @param array $args
919
  * @return object
920
- */
921
  public function campaign_action( $campaign_id, $action, array $args = array() ) {
922
  $resource = sprintf( '/campaigns/%s/actions/%s', $campaign_id, $action );
923
  return $this->client->post( $resource, $args );
@@ -925,12 +925,12 @@ class MC4WP_API_v3 {
925
 
926
  /**
927
  * Get the HTML and plain-text content for a campaign
928
- *
929
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#read-get_campaigns_campaign_id_content
930
  * @param string $campaign_id
931
  * @param array $args
932
  * @return object
933
- */
934
  public function get_campaign_content( $campaign_id, array $args = array() ) {
935
  $resource = sprintf( '/campaigns/%s/content', $campaign_id );
936
  return $this->client->get( $resource, $args );
@@ -941,9 +941,9 @@ class MC4WP_API_v3 {
941
  *
942
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#edit-put_campaigns_campaign_id_content
943
  * @param string $campaign_id
944
- * @param array $args
945
  * @return object
946
- */
947
  public function update_campaign_content( $campaign_id, array $args ) {
948
  $resource = sprintf( '/campaigns/%s/content', $campaign_id );
949
  return $this->client->put( $resource, $args );
@@ -964,4 +964,4 @@ class MC4WP_API_v3 {
964
  }
965
 
966
 
967
- }
830
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/templates/#read-get_templates_template_id
831
  * @param string $template_id
832
  * @return object
833
+ */
834
  public function get_template( $template_id, array $args = array() ) {
835
  $resource = sprintf( '/templates/%s', $template_id );
836
  return $this->client->get( $resource, $args );
840
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/templates/default-content/
841
  * @param string $template_id
842
  * @return object
843
+ */
844
  public function get_template_default_content( $template_id, array $args = array() ) {
845
  $resource = sprintf( '/templates/%s/default-content', $template_id );
846
  return $this->client->get( $resource, $args );
852
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#create-post_campaigns
853
  * @param array $args
854
  * @return object
855
+ */
856
  public function add_campaign( array $args ) {
857
  $resource = '/campaigns';
858
  return $this->client->post( $resource, $args );
864
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#read-get_campaigns
865
  * @param array $args
866
  * @return object
867
+ */
868
  public function get_campaigns( array $args = array() ) {
869
  $resource = '/campaigns';
870
  return $this->client->get( $resource, $args );
877
  * @param string $campaign_id
878
  * @param array $args
879
  * @return object
880
+ */
881
  public function get_campaign( $campaign_id, array $args = array() ) {
882
  $resource = sprintf( '/campaigns/%s', $campaign_id );
883
+ return $this->client->get( $resource, $args );
884
  }
885
 
886
  /**
890
  * @param string $campaign_id
891
  * @param array $args
892
  * @return object
893
+ */
894
  public function update_campaign( $campaign_id, array $args ) {
895
  $resource = sprintf( '/campaigns/%s', $campaign_id );
896
  return $this->client->patch( $resource, $args );
902
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#delete-delete_campaigns_campaign_id
903
  * @param string $campaign_id
904
  * @return bool
905
+ */
906
  public function delete_campaign( $campaign_id ) {
907
  $resource = sprintf( '/campaigns/%s', $campaign_id );
908
  return !! $this->client->delete( $resource );
914
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns
915
  *
916
  * @param string $campaign_id
917
+ * @param string $action
918
  * @param array $args
919
  * @return object
920
+ */
921
  public function campaign_action( $campaign_id, $action, array $args = array() ) {
922
  $resource = sprintf( '/campaigns/%s/actions/%s', $campaign_id, $action );
923
  return $this->client->post( $resource, $args );
925
 
926
  /**
927
  * Get the HTML and plain-text content for a campaign
928
+ *
929
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#read-get_campaigns_campaign_id_content
930
  * @param string $campaign_id
931
  * @param array $args
932
  * @return object
933
+ */
934
  public function get_campaign_content( $campaign_id, array $args = array() ) {
935
  $resource = sprintf( '/campaigns/%s/content', $campaign_id );
936
  return $this->client->get( $resource, $args );
941
  *
942
  * @link https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#edit-put_campaigns_campaign_id_content
943
  * @param string $campaign_id
944
+ * @param array $args
945
  * @return object
946
+ */
947
  public function update_campaign_content( $campaign_id, array $args ) {
948
  $resource = sprintf( '/campaigns/%s/content', $campaign_id );
949
  return $this->client->put( $resource, $args );
964
  }
965
 
966
 
967
+ }
includes/api/class-exception.php CHANGED
@@ -6,6 +6,11 @@ class MC4WP_API_Exception extends Exception {
6
  * @var array
7
  */
8
  public $response;
 
 
 
 
 
9
  public $type = '';
10
  public $title = '';
11
  public $status = '';
@@ -25,6 +30,7 @@ class MC4WP_API_Exception extends Exception {
25
  parent::__construct( $message, $code );
26
 
27
  $this->response = $response;
 
28
 
29
  if( ! empty( $data ) ) {
30
  // fill error properties from json data
@@ -43,11 +49,12 @@ class MC4WP_API_Exception extends Exception {
43
  public function __toString() {
44
  $string = $this->message . '.';
45
 
 
46
  if( ! empty( $this->detail ) ) {
47
  $string .= ' ' . $this->detail;
48
  }
49
 
50
-
51
  if( ! empty( $this->errors ) && isset( $this->errors[0]->field ) ) {
52
 
53
  // strip off obsolete msg
@@ -68,4 +75,4 @@ class MC4WP_API_Exception extends Exception {
68
 
69
  return $string;
70
  }
71
- }
6
  * @var array
7
  */
8
  public $response;
9
+
10
+ /**
11
+ * @var mixed
12
+ */
13
+ public $data;
14
  public $type = '';
15
  public $title = '';
16
  public $status = '';
30
  parent::__construct( $message, $code );
31
 
32
  $this->response = $response;
33
+ $this->data = $data;
34
 
35
  if( ! empty( $data ) ) {
36
  // fill error properties from json data
49
  public function __toString() {
50
  $string = $this->message . '.';
51
 
52
+ // add detail message
53
  if( ! empty( $this->detail ) ) {
54
  $string .= ' ' . $this->detail;
55
  }
56
 
57
+ // add field specific errors
58
  if( ! empty( $this->errors ) && isset( $this->errors[0]->field ) ) {
59
 
60
  // strip off obsolete msg
75
 
76
  return $string;
77
  }
78
+ }
includes/class-list-data-mapper.php CHANGED
@@ -1,33 +1,33 @@
1
  <?php
2
 
3
  /**
4
- * Class MC4WP_Field_Map
5
- *
6
- * @access private
7
- * @since 4.0
8
- * @ignore
9
- */
10
  class MC4WP_List_Data_Mapper {
11
 
12
  /**
13
- * @var array
14
- */
15
  private $data = array();
16
 
17
  /**
18
- * @var array
19
- */
20
  private $list_ids = array();
21
 
22
- /**
23
- * @var MC4WP_Field_Formatter
24
- */
25
- private $formatter;
26
 
27
  /**
28
- * @param array $data
29
- * @param array $list_ids
30
- */
31
  public function __construct( array $data, array $list_ids ) {
32
  $this->data = array_change_key_case( $data, CASE_UPPER );
33
  $this->list_ids = $list_ids;
@@ -39,8 +39,8 @@ class MC4WP_List_Data_Mapper {
39
  }
40
 
41
  /**
42
- * @return MC4WP_MailChimp_Subscriber[]
43
- */
44
  public function map() {
45
  $mailchimp = new MC4WP_MailChimp();
46
  $map = array();
@@ -57,10 +57,10 @@ class MC4WP_List_Data_Mapper {
57
  }
58
 
59
  /**
60
- * @param MC4WP_MailChimp_List $list
61
- *
62
- * @return MC4WP_MailChimp_Subscriber
63
- */
64
  protected function map_list( MC4WP_MailChimp_List $list ) {
65
 
66
  $subscriber = new MC4WP_MailChimp_Subscriber();
@@ -87,55 +87,55 @@ class MC4WP_List_Data_Mapper {
87
  }
88
 
89
  // find interest categories
90
- if( ! empty( $this->data['INTERESTS'] ) ) {
91
- foreach( $list->interest_categories as $interest_category ) {
92
- foreach( $interest_category->interests as $interest_id => $interest_name ) {
93
-
94
- // straight lookup by ID as key with value copy.
95
- if( isset( $this->data['INTERESTS'][ $interest_id ] ) ) {
96
- $subscriber->interests[ $interest_id ] = $this->formatter->boolean( $this->data['INTERESTS'][ $interest_id ] );
97
- }
98
-
99
- // straight lookup by ID as top-level value
100
- if( in_array( $interest_id, $this->data['INTERESTS'], false ) ) {
101
- $subscriber->interests[ $interest_id ] = true;
102
- }
103
-
104
- // look in array with category ID as key.
105
- if( isset( $this->data['INTERESTS'][ $interest_category->id ] ) ) {
106
- $value = $this->data['INTERESTS'][ $interest_category->id ];
107
- $values = is_array( $value ) ? $value : array_map( 'trim', explode( '|', $value ) );
108
-
109
- // find by category ID + interest ID
110
- if( in_array( $interest_id, $values, false ) ) {
111
- $subscriber->interests[ $interest_id ] = true;
112
- }
113
-
114
- // find by category ID + interest name
115
- if( in_array( $interest_name, $values ) ) {
116
- $subscriber->interests[ $interest_id ] = true;
117
- }
118
- }
119
- }
120
- }
121
- }
122
-
123
- // find language
124
- /* @see http://kb.mailchimp.com/lists/managing-subscribers/view-and-edit-subscriber-languages?utm_source=mc-api&utm_medium=docs&utm_campaign=apidocs&_ga=1.211519638.2083589671.1469697070 */
125
- if( ! empty( $this->data['MC_LANGUAGE'] ) ) {
126
- $subscriber->language = $this->formatter->language( $this->data['MC_LANGUAGE'] );
127
- }
128
 
129
  return $subscriber;
130
  }
131
 
132
 
133
  /**
134
- * @param mixed $field_value
135
- * @param string $field_type
136
- *
137
- * @return mixed
138
- */
139
  private function format_merge_field_value( $field_value, $field_type ) {
140
  $field_type = strtolower( $field_type );
141
 
@@ -144,17 +144,17 @@ class MC4WP_List_Data_Mapper {
144
  }
145
 
146
  /**
147
- * Filters the value of a field after it is formatted.
148
- *
149
- * Use this to format a field value according to the field type (in MailChimp).
150
- *
151
- * @since 3.0
152
- * @param string $field_value The value
153
- * @param string $field_type The type of the field (in MailChimp)
154
- */
155
  $field_value = apply_filters( 'mc4wp_format_field_value', $field_value, $field_type );
156
 
157
  return $field_value;
158
  }
159
 
160
- }
1
  <?php
2
 
3
  /**
4
+ * Class MC4WP_Field_Map
5
+ *
6
+ * @access private
7
+ * @since 4.0
8
+ * @ignore
9
+ */
10
  class MC4WP_List_Data_Mapper {
11
 
12
  /**
13
+ * @var array
14
+ */
15
  private $data = array();
16
 
17
  /**
18
+ * @var array
19
+ */
20
  private $list_ids = array();
21
 
22
+ /**
23
+ * @var MC4WP_Field_Formatter
24
+ */
25
+ private $formatter;
26
 
27
  /**
28
+ * @param array $data
29
+ * @param array $list_ids
30
+ */
31
  public function __construct( array $data, array $list_ids ) {
32
  $this->data = array_change_key_case( $data, CASE_UPPER );
33
  $this->list_ids = $list_ids;
39
  }
40
 
41
  /**
42
+ * @return MC4WP_MailChimp_Subscriber[]
43
+ */
44
  public function map() {
45
  $mailchimp = new MC4WP_MailChimp();
46
  $map = array();
57
  }
58
 
59
  /**
60
+ * @param MC4WP_MailChimp_List $list
61
+ *
62
+ * @return MC4WP_MailChimp_Subscriber
63
+ */
64
  protected function map_list( MC4WP_MailChimp_List $list ) {
65
 
66
  $subscriber = new MC4WP_MailChimp_Subscriber();
87
  }
88
 
89
  // find interest categories
90
+ if( ! empty( $this->data['INTERESTS'] ) ) {
91
+ foreach( $list->interest_categories as $interest_category ) {
92
+ foreach( $interest_category->interests as $interest_id => $interest_name ) {
93
+
94
+ // straight lookup by ID as key with value copy.
95
+ if( isset( $this->data['INTERESTS'][ $interest_id ] ) ) {
96
+ $subscriber->interests[ $interest_id ] = $this->formatter->boolean( $this->data['INTERESTS'][ $interest_id ] );
97
+ }
98
+
99
+ // straight lookup by ID as top-level value
100
+ if( in_array( $interest_id, $this->data['INTERESTS'], false ) ) {
101
+ $subscriber->interests[ $interest_id ] = true;
102
+ }
103
+
104
+ // look in array with category ID as key.
105
+ if( isset( $this->data['INTERESTS'][ $interest_category->id ] ) ) {
106
+ $value = $this->data['INTERESTS'][ $interest_category->id ];
107
+ $values = is_array( $value ) ? $value : array_map( 'trim', explode( '|', $value ) );
108
+
109
+ // find by category ID + interest ID
110
+ if( in_array( $interest_id, $values, false ) ) {
111
+ $subscriber->interests[ $interest_id ] = true;
112
+ }
113
+
114
+ // find by category ID + interest name
115
+ if( in_array( $interest_name, $values ) ) {
116
+ $subscriber->interests[ $interest_id ] = true;
117
+ }
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ // find language
124
+ /* @see http://kb.mailchimp.com/lists/managing-subscribers/view-and-edit-subscriber-languages?utm_source=mc-api&utm_medium=docs&utm_campaign=apidocs&_ga=1.211519638.2083589671.1469697070 */
125
+ if( ! empty( $this->data['MC_LANGUAGE'] ) ) {
126
+ $subscriber->language = $this->formatter->language( $this->data['MC_LANGUAGE'] );
127
+ }
128
 
129
  return $subscriber;
130
  }
131
 
132
 
133
  /**
134
+ * @param mixed $field_value
135
+ * @param string $field_type
136
+ *
137
+ * @return mixed
138
+ */
139
  private function format_merge_field_value( $field_value, $field_type ) {
140
  $field_type = strtolower( $field_type );
141
 
144
  }
145
 
146
  /**
147
+ * Filters the value of a field after it is formatted.
148
+ *
149
+ * Use this to format a field value according to the field type (in MailChimp).
150
+ *
151
+ * @since 3.0
152
+ * @param string $field_value The value
153
+ * @param string $field_type The type of the field (in MailChimp)
154
+ */
155
  $field_value = apply_filters( 'mc4wp_format_field_value', $field_value, $field_type );
156
 
157
  return $field_value;
158
  }
159
 
160
+ }
includes/class-mailchimp.php CHANGED
@@ -1,47 +1,47 @@
1
  <?php
2
 
3
  /**
4
- * Class MC4WP_MailChimp
5
- *
6
- * @access private
7
- * @ignore
8
- */
9
  class MC4WP_MailChimp {
10
 
11
  /**
12
- * @var MC4WP_API_v3
13
- */
14
  public $api;
15
 
16
  /**
17
- * @var string
18
- */
19
  public $error_code = '';
20
 
21
  /**
22
- * @var string
23
- */
24
  public $error_message = '';
25
 
26
  /**
27
- * MC4WP_MailChimp constructor.
28
- */
29
  public function __construct() {
30
  $this->api = mc4wp( 'api' );
31
  }
32
 
33
  /**
34
- *
35
- * Sends a subscription request to the MailChimp API
36
- *
37
- * @param string $list_id The list id to subscribe to
38
- * @param string $email_address The email address to subscribe
39
- * @param array $args
40
- * @param boolean $update_existing Update information if this email is already on list?
41
- * @param boolean $replace_interests Replace interest groupings, only if update_existing is true.
42
- *
43
- * @return object
44
- */
45
  public function list_subscribe( $list_id, $email_address, array $args = array(), $update_existing = false, $replace_interests = true ) {
46
  $this->reset_error();
47
 
@@ -51,7 +51,7 @@ class MC4WP_MailChimp {
51
  'interests' => array(),
52
  'merge_fields' => array(),
53
  );
54
- $already_on_list = false;
55
 
56
  // setup default args
57
  $args = $args + $default_args;
@@ -61,7 +61,7 @@ class MC4WP_MailChimp {
61
  $existing_member_data = $this->api->get_list_member( $list_id, $email_address );
62
 
63
  if( $existing_member_data->status === 'subscribed' ) {
64
- $already_on_list = true;
65
 
66
  // if we're not supposed to update, bail.
67
  if( ! $update_existing ) {
@@ -84,15 +84,15 @@ class MC4WP_MailChimp {
84
  $args['interests'] = $args['interests'] + $existing_interests;
85
  }
86
  } else {
87
- // delete list member so we can re-add it...
88
- $this->api->delete_list_member( $list_id, $email_address );
89
- }
90
  } catch ( MC4WP_API_Resource_Not_Found_Exception $e ) {
91
  // subscriber does not exist (not an issue in this case)
92
  } catch( MC4WP_API_Exception $e ) {
93
- // other errors.
94
- $this->error_code = $e->getCode();
95
- $this->error_message = $e;
96
  return null;
97
  }
98
 
@@ -110,21 +110,21 @@ class MC4WP_MailChimp {
110
  }
111
 
112
  /**
113
- *
114
- * @param string $list_id
115
- * @param string $email_address
116
- *
117
- * @return boolean
118
- */
119
  public function list_unsubscribe( $list_id, $email_address ) {
120
  $this->reset_error();
121
 
122
  try {
123
  $this->api->update_list_member( $list_id, $email_address, array( 'status' => 'unsubscribed' ) );
124
  } catch( MC4WP_API_Resource_Not_Found_Exception $e ) {
125
- // if email wasn't even on the list: great.
126
- return true;
127
- } catch( MC4WP_API_Exception $e ) {
128
  $this->error_code = $e->getCode();
129
  $this->error_message = $e;
130
  return false;
@@ -134,27 +134,27 @@ class MC4WP_MailChimp {
134
  }
135
 
136
  /**
137
- * Checks if an email address is on a given list with status "subscribed"
138
- *
139
- * @param string $list_id
140
- * @param string $email_address
141
- *
142
- * @return boolean
143
- */
144
  public function list_has_subscriber( $list_id, $email_address ) {
145
- try{
146
- $data = $this->api->get_list_member( $list_id, $email_address );
147
- } catch( MC4WP_API_Resource_Not_Found_Exception $e ) {
148
- return false;
149
- }
150
 
151
  return ! empty( $data->id ) && $data->status === 'subscribed';
152
  }
153
 
154
 
155
  /**
156
- * Empty the Lists cache
157
- */
158
  public function empty_cache() {
159
  global $wpdb;
160
 
@@ -163,32 +163,32 @@ class MC4WP_MailChimp {
163
  delete_transient( 'mc4wp_list_counts' );
164
  }
165
 
166
- /**
167
- * Get MailChimp lists from cache.
168
- *
169
- * @param boolean deprecated parameter.
170
- * @return array
171
- */
172
  public function get_cached_lists() {
173
  return $this->get_lists( false );
174
  }
175
 
176
  /**
177
- * Get a specific MailChimp list from local DB.
178
- *
179
- * @param string $list_id
180
- * @return MC4WP_MailChimp_List
181
- */
182
  public function get_cached_list( $list_id ) {
183
  return $this->get_list( $list_id, false );
184
  }
185
 
186
  /**
187
- * Get MailChimp lists, from cache or remote API.
188
- *
189
- * @param boolean $force Whether to force a result by hitting MailChimp API
190
- * @return array
191
- */
192
  public function get_lists( $force = true ) {
193
 
194
  // first, get all list id's
@@ -204,11 +204,11 @@ class MC4WP_MailChimp {
204
  return $lists;
205
  }
206
 
207
- /**
208
- * @param string $list_id
209
- *
210
- * @return MC4WP_MailChimp_List
211
- */
212
  private function fetch_list( $list_id ) {
213
  try{
214
  $list_data = $this->api->get_list( $list_id, array( 'fields' => 'id,name,stats,web_id,campaign_defaults.from_name,campaign_defaults.from_email' ) );
@@ -246,53 +246,53 @@ class MC4WP_MailChimp {
246
  } catch( MC4WP_API_Exception $e ) {
247
  return null;
248
  }
249
-
250
  // save in option
251
- update_option( 'mc4wp_mailchimp_list_' . $list_id, $list, false );
252
- return $list;
253
  }
254
 
255
  /**
256
- * Get MailChimp list ID's
257
- *
258
- * @param bool $force Force result by hitting MailChimp API
259
- * @return array
260
- */
261
  public function get_list_ids( $force = false ) {
262
  $list_ids = (array) get_option( 'mc4wp_mailchimp_list_ids', array() );
263
 
264
  if( empty( $list_ids ) && $force ) {
265
- $list_ids = $this->fetch_list_ids();
266
  }
267
-
268
  return $list_ids;
269
  }
270
 
271
  /**
272
- * @return array
273
- */
274
  public function fetch_list_ids() {
275
- try{
276
- $lists_data = $this->api->get_lists( array( 'count' => 200, 'fields' => 'lists.id' ) );
277
- } catch( MC4WP_API_Exception $e ) {
278
- return array();
279
- }
280
 
281
  $list_ids = wp_list_pluck( $lists_data, 'id' );
282
 
283
- // store list id's
284
  update_option( 'mc4wp_mailchimp_list_ids', $list_ids, false );
285
-
286
  return $list_ids;
287
  }
288
 
289
- /**
290
- * Fetch list ID's + lists from MailChimp.
291
- *
292
- * @return bool
293
- */
294
  public function fetch_lists() {
295
- // try to increase time limit as this can take a while
296
  @set_time_limit(300);
297
  $list_ids = $this->fetch_list_ids();
298
 
@@ -305,15 +305,15 @@ class MC4WP_MailChimp {
305
  }
306
 
307
  return ! empty( $list_ids );
308
- }
309
 
310
  /**
311
- * Get a given MailChimp list
312
- *
313
- * @param string $list_id
314
- * @param bool $force Whether to force a result by hitting remote API
315
- * @return MC4WP_MailChimp_List
316
- */
317
  public function get_list( $list_id, $force = false ) {
318
  $list = get_option( 'mc4wp_mailchimp_list_' . $list_id );
319
 
@@ -322,17 +322,17 @@ class MC4WP_MailChimp {
322
  }
323
 
324
  if( empty( $list ) ) {
325
- return new MC4WP_MailChimp_List( '', 'Unknown List' );
326
  }
327
 
328
  return $list;
329
  }
330
 
331
  /**
332
- * Get an array of list_id => number of subscribers
333
- *
334
- * @return array
335
- */
336
  public function get_subscriber_counts() {
337
 
338
  // get from transient
@@ -342,11 +342,11 @@ class MC4WP_MailChimp {
342
  }
343
 
344
  // transient not valid, fetch from API
345
- try {
346
- $lists = $this->api->get_lists( array( 'count' => 100, 'fields' => 'lists.id,lists.stats' ) );
347
- } catch( MC4WP_API_Exception $e ) {
348
- return array();
349
- }
350
 
351
  $list_counts = array();
352
 
@@ -358,11 +358,11 @@ class MC4WP_MailChimp {
358
  $seconds = 3600;
359
 
360
  /**
361
- * Filters the cache time for MailChimp lists configuration, in seconds. Defaults to 3600 seconds (1 hour).
362
- *
363
- * @since 2.0
364
- * @param int $seconds
365
- */
366
  $transient_lifetime = (int) apply_filters( 'mc4wp_lists_count_cache_time', $seconds );
367
  set_transient( 'mc4wp_list_counts', $list_counts, $transient_lifetime );
368
 
@@ -372,11 +372,11 @@ class MC4WP_MailChimp {
372
 
373
 
374
  /**
375
- * Returns number of subscribers on given lists.
376
- *
377
- * @param array|string $list_ids Array of list ID's, or single string.
378
- * @return int Total # subscribers for given lists.
379
- */
380
  public function get_subscriber_count( $list_ids ) {
381
 
382
  // make sure we're getting an array
@@ -399,40 +399,40 @@ class MC4WP_MailChimp {
399
  }
400
 
401
  /**
402
- * Filters the total subscriber_count for the given List ID's.
403
- *
404
- * @since 2.0
405
- * @param string $count
406
- * @param array $list_ids
407
- */
408
  return apply_filters( 'mc4wp_subscriber_count', $count, $list_ids );
409
  }
410
 
411
  /**
412
- * Resets error properties.
413
- */
414
  public function reset_error() {
415
  $this->error_message = '';
416
  $this->error_code = '';
417
  }
418
 
419
  /**
420
- * @return bool
421
- */
422
  public function has_error() {
423
  return ! empty( $this->error_code );
424
  }
425
 
426
  /**
427
- * @return string
428
- */
429
  public function get_error_message() {
430
  return $this->error_message;
431
  }
432
 
433
  /**
434
- * @return string
435
- */
436
  public function get_error_code() {
437
  return $this->error_code;
438
  }
1
  <?php
2
 
3
  /**
4
+ * Class MC4WP_MailChimp
5
+ *
6
+ * @access private
7
+ * @ignore
8
+ */
9
  class MC4WP_MailChimp {
10
 
11
  /**
12
+ * @var MC4WP_API_v3
13
+ */
14
  public $api;
15
 
16
  /**
17
+ * @var string
18
+ */
19
  public $error_code = '';
20
 
21
  /**
22
+ * @var string
23
+ */
24
  public $error_message = '';
25
 
26
  /**
27
+ * MC4WP_MailChimp constructor.
28
+ */
29
  public function __construct() {
30
  $this->api = mc4wp( 'api' );
31
  }
32
 
33
  /**
34
+ *
35
+ * Sends a subscription request to the MailChimp API
36
+ *
37
+ * @param string $list_id The list id to subscribe to
38
+ * @param string $email_address The email address to subscribe
39
+ * @param array $args
40
+ * @param boolean $update_existing Update information if this email is already on list?
41
+