MailChimp for WooCommerce - Version 1.1.1

Version Description

  • Support for site url changes
  • Fix for WP Version 4.4 compatibility issues
Download this release

Release Info

Developer MailChimp
Plugin Icon wp plugin MailChimp for WooCommerce
Version 1.1.1
Comparing to
See all releases

Code changes from version 1.1.0 to 1.1.1

README.txt CHANGED
@@ -21,7 +21,7 @@ You’ll have the power to:
21
  - Send product recommendations
22
  - Segment based on purchase history
23
  - View your results and measure ROI
24
- - Grow your audience and sell more stuff with Facebook Ad Campaigns in MailChimp (New)
25
 
26
  ###A note for current WooCommerce integration users
27
  This plugin supports our most powerful API 3.0 features, and is intended for users who have not yet integrated their WooCommerce stores with MailChimp.
@@ -49,6 +49,10 @@ You’ll need to do a few things to connect your WooCommerce store to MailChimp.
49
  For more information on settings and configuration, please visit our Knowledge Base: [http://kb.mailchimp.com/integrations/e-commerce/connect-or-disconnect-mailchimp-for-woocommerce](http://kb.mailchimp.com/integrations/e-commerce/connect-or-disconnect-mailchimp-for-woocommerce)
50
 
51
  == Changelog ==
 
 
 
 
52
  = 1.1.0 =
53
  * Fix for persisting opt-in status
54
  * Pass order URLs to MailChimp
21
  - Send product recommendations
22
  - Segment based on purchase history
23
  - View your results and measure ROI
24
+ - Grow your audience and sell more stuff with Facebook Ad Campaigns in MailChimp
25
 
26
  ###A note for current WooCommerce integration users
27
  This plugin supports our most powerful API 3.0 features, and is intended for users who have not yet integrated their WooCommerce stores with MailChimp.
49
  For more information on settings and configuration, please visit our Knowledge Base: [http://kb.mailchimp.com/integrations/e-commerce/connect-or-disconnect-mailchimp-for-woocommerce](http://kb.mailchimp.com/integrations/e-commerce/connect-or-disconnect-mailchimp-for-woocommerce)
50
 
51
  == Changelog ==
52
+ = 1.1.1 =
53
+ * Support for site url changes
54
+ * Fix for WP Version 4.4 compatibility issues
55
+
56
  = 1.1.0 =
57
  * Fix for persisting opt-in status
58
  * Pass order URLs to MailChimp
changelog.md CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  ** 1.1.0 **
2
  * Fix for persisting opt-in status
3
  * Pass order URLs to MailChimp
1
+ ** 1.1.1 **
2
+ * Support for site url changes
3
+ * Fix for WP Version 4.4 Compatibility issues
4
+
5
  ** 1.1.0 **
6
  * Fix for persisting opt-in status
7
  * Pass order URLs to MailChimp
includes/class-mailchimp-woocommerce-activator.php CHANGED
@@ -28,58 +28,11 @@ class MailChimp_Woocommerce_Activator {
28
  // create the queue tables because we need them for the sync jobs.
29
  static::create_queue_tables();
30
 
31
- // create the api keys for MC just in case.
32
- $data = static::create_keys('MailChimp', 'USER_ID', 'read_write');
33
-
34
  // update the settings so we have them for use.
35
- update_option('mailchimp-woocommerce', array(
36
- 'woo_consumer_key' => $data['consumer_key'],
37
- 'woo_consumer_secret' => $data['consumer_secret']
38
- ));
39
- }
40
-
41
- /**
42
- * Create keys.
43
- *
44
- * @since 2.4.0
45
- *
46
- * @param string $app_name
47
- * @param string $app_user_id
48
- * @param string $scope
49
- *
50
- * @return array
51
- */
52
- public static function create_keys( $app_name, $app_user_id, $scope ) {
53
- global $wpdb;
54
 
55
- $description = sprintf( __( '%s - API %s (created on %s at %s).', 'woocommerce' ), wc_clean( $app_name ), __( 'Read/Write', 'woocommerce' ), date_i18n( wc_date_format() ), date_i18n( wc_time_format() ) );
56
- $user = wp_get_current_user();
57
-
58
- // Created API keys.
59
- $permissions = 'read_write';
60
- $consumer_key = 'ck_' . wc_rand_hash();
61
- $consumer_secret = 'cs_' . wc_rand_hash();
62
-
63
- $wpdb->insert(
64
- $wpdb->prefix . 'woocommerce_api_keys',
65
- array(
66
- 'user_id' => $user->ID,
67
- 'description' => $description,
68
- 'permissions' => $permissions,
69
- 'consumer_key' => wc_api_hash( $consumer_key ),
70
- 'consumer_secret' => $consumer_secret,
71
- 'truncated_key' => substr( $consumer_key, -7 )
72
- ),
73
- array('%d', '%s', '%s', '%s', '%s', '%s')
74
- );
75
-
76
- return array(
77
- 'key_id' => $wpdb->insert_id,
78
- 'user_id' => $app_user_id,
79
- 'consumer_key' => $consumer_key,
80
- 'consumer_secret' => $consumer_secret,
81
- 'key_permissions' => $permissions
82
- );
83
  }
84
 
85
  /**
28
  // create the queue tables because we need them for the sync jobs.
29
  static::create_queue_tables();
30
 
 
 
 
31
  // update the settings so we have them for use.
32
+ update_option('mailchimp-woocommerce', array());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ // add a store id flag which will be a random hash
35
+ update_option('mailchimp-woocommerce-store_id', md5(get_option('siteurl')), 'yes');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  }
37
 
38
  /**
includes/class-mailchimp-woocommerce-options.php CHANGED
@@ -63,7 +63,7 @@ abstract class MailChimp_Woocommerce_Options
63
  */
64
  public function getUniqueStoreID()
65
  {
66
- return md5(get_option('siteurl'));
67
  }
68
 
69
  /**
63
  */
64
  public function getUniqueStoreID()
65
  {
66
+ return mailchimp_get_store_id();
67
  }
68
 
69
  /**
includes/class-mailchimp-woocommerce-service.php CHANGED
@@ -370,7 +370,9 @@ class MailChimp_Service extends MailChimp_Woocommerce_Options
370
  // if we already have a cookie here, we need to skip it.
371
  if ($this->getLandingSiteCookie() != false) return $this;
372
 
373
- if (($http_referer = wp_get_raw_referer()) && !empty($http_referer)) {
 
 
374
 
375
  // grab the current landing url since it's a referral.
376
  $landing_site = home_url() . wp_unslash($_SERVER['REQUEST_URI']);
@@ -389,6 +391,19 @@ class MailChimp_Service extends MailChimp_Woocommerce_Options
389
  return $this;
390
  }
391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
  /**
393
  * @return $this
394
  */
@@ -546,18 +561,6 @@ class MailChimp_Service extends MailChimp_Woocommerce_Options
546
  return $_REQUEST['mailchimp-woocommerce'][$key];
547
  }
548
 
549
- /**
550
- * @return bool
551
- */
552
- protected function authenticate()
553
- {
554
- if (trim((string) $this->getUniqueStoreID()) !== trim((string) $this->get('store_id'))) {
555
- $this->respondJSON(array('success' => false, 'message' => 'Not Authorized'));
556
- }
557
-
558
- return true;
559
- }
560
-
561
  /**
562
  * @param $uid
563
  * @return array|bool|null|object|void
370
  // if we already have a cookie here, we need to skip it.
371
  if ($this->getLandingSiteCookie() != false) return $this;
372
 
373
+ $http_referer = $this->getReferer();
374
+
375
+ if (!empty($http_referer)) {
376
 
377
  // grab the current landing url since it's a referral.
378
  $landing_site = home_url() . wp_unslash($_SERVER['REQUEST_URI']);
391
  return $this;
392
  }
393
 
394
+ /**
395
+ * @return array|bool|string
396
+ */
397
+ public function getReferer()
398
+ {
399
+ if (!empty($_REQUEST['_wp_http_referer'])) {
400
+ return wp_unslash($_REQUEST['_wp_http_referer']);
401
+ } elseif (!empty($_SERVER['HTTP_REFERER'])) {
402
+ return wp_unslash( $_SERVER['HTTP_REFERER']);
403
+ }
404
+ return false;
405
+ }
406
+
407
  /**
408
  * @return $this
409
  */
561
  return $_REQUEST['mailchimp-woocommerce'][$key];
562
  }
563
 
 
 
 
 
 
 
 
 
 
 
 
 
564
  /**
565
  * @param $uid
566
  * @return array|bool|null|object|void
includes/processes/class-mailchimp-woocommerce-abstract-sync.php CHANGED
@@ -59,7 +59,7 @@ abstract class MailChimp_WooCommerce_Abtstract_Sync extends WP_Job
59
  */
60
  public function getStoreID()
61
  {
62
- return md5(get_option('siteurl'));
63
  }
64
 
65
  /**
59
  */
60
  public function getStoreID()
61
  {
62
+ return mailchimp_get_store_id();
63
  }
64
 
65
  /**
mailchimp-woocommerce.php CHANGED
@@ -16,7 +16,7 @@
16
  * Plugin Name: MailChimp for WooCommerce
17
  * Plugin URI: https://mailchimp.com/connect-your-store/
18
  * Description: MailChimp - WooCommerce plugin
19
- * Version: 1.1.0
20
  * Author: MailChimp
21
  * Author URI: https://mailchimp.com
22
  * License: GPL-2.0+
@@ -27,52 +27,59 @@
27
 
28
  // If this file is called directly, abort.
29
  if ( ! defined( 'WPINC' ) ) {
30
- die;
31
  }
32
 
33
  /**
34
  * @return object
35
  */
36
  function mailchimp_environment_variables() {
37
- global $wp_version;
38
-
39
- return (object) array(
40
- 'repo' => 'master',
41
- 'environment' => 'production',
42
- 'version' => '1.1.0',
43
- 'wp_version' => (empty($wp_version) ? 'Unknown' : $wp_version),
44
- );
45
  }
46
 
47
  /**
48
  * @return bool|int
49
  */
50
  function mailchimp_get_list_id() {
51
- if (($options = get_option('mailchimp-woocommerce', false)) && is_array($options)) {
52
- if (isset($options['mailchimp_list'])) {
53
- return $options['mailchimp_list'];
54
- }
55
- }
56
- return false;
57
  }
58
 
59
  /**
60
  * @return string
61
  */
62
  function mailchimp_get_store_id() {
63
- return md5(get_option('siteurl'));
 
 
 
 
 
 
 
64
  }
65
 
66
  /**
67
  * @return bool|MailChimp_WooCommerce_MailChimpApi
68
  */
69
  function mailchimp_get_api() {
70
- if (($options = get_option('mailchimp-woocommerce', false)) && is_array($options)) {
71
- if (isset($options['mailchimp_api_key'])) {
72
- return new MailChimp_WooCommerce_MailChimpApi($options['mailchimp_api_key']);
73
- }
74
- }
75
- return false;
76
  }
77
 
78
  /**
@@ -81,23 +88,33 @@ function mailchimp_get_api() {
81
  * @return null
82
  */
83
  function mailchimp_get_option($key, $default = null) {
84
- $options = get_option('mailchimp-woocommerce');
85
- if (!is_array($options)) {
86
- return $default;
87
- }
88
- if (!array_key_exists($key, $options)) {
89
- return $default;
90
- }
91
- return $options[$key];
92
  }
93
 
94
  /**
95
  * @param $key
96
- * @param $default
97
- * @return mixed|void
98
  */
99
- function mailchimp_get_data($key, $default) {
100
- return get_option('mailchimp-woocommerce-'.$key, $default);
 
 
 
 
 
 
 
 
 
 
101
  }
102
 
103
  /**
@@ -105,18 +122,18 @@ function mailchimp_get_data($key, $default) {
105
  * @return DateTime
106
  */
107
  function mailchimp_date_utc($date) {
108
- $timezone = wc_timezone_string();
109
- //$timezone = mailchimp_get_option('store_timezone', 'America/New_York');
110
- if (is_numeric($date)) {
111
- $stamp = $date;
112
- $date = new \DateTime('now', new DateTimeZone($timezone));
113
- $date->setTimestamp($stamp);
114
- } else {
115
- $date = new \DateTime($date, new DateTimeZone($timezone));
116
- }
117
-
118
- $date->setTimezone(new DateTimeZone('UTC'));
119
- return $date;
120
  }
121
 
122
  /**
@@ -125,13 +142,13 @@ function mailchimp_date_utc($date) {
125
  */
126
  function mailchimp_date_local($date) {
127
  $timezone = mailchimp_get_option('store_timezone', 'America/New_York');
128
- if (is_numeric($date)) {
129
- $stamp = $date;
130
- $date = new \DateTime('now', new DateTimeZone('UTC'));
131
- $date->setTimestamp($stamp);
132
- } else {
133
- $date = new \DateTime($date, new DateTimeZone('UTC'));
134
- }
135
 
136
  $date->setTimezone(new DateTimeZone($timezone));
137
  return $date;
@@ -142,34 +159,34 @@ function mailchimp_date_local($date) {
142
  * @return mixed
143
  */
144
  function mailchimp_array_remove_empty($data) {
145
- if (empty($data) || !is_array($data)) {
146
- return array();
147
- }
148
- foreach ($data as $key => $value) {
149
- if ($value === null || $value === '') {
150
- unset($data[$key]);
151
- }
152
- }
153
- return $data;
154
  }
155
 
156
  /**
157
  * @return array
158
  */
159
  function mailchimp_get_timezone_list() {
160
- $zones_array = array();
161
- $timestamp = time();
162
- $current = date_default_timezone_get();
163
 
164
- foreach(timezone_identifiers_list() as $key => $zone) {
165
- date_default_timezone_set($zone);
166
- $zones_array[$key]['zone'] = $zone;
167
- $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
168
- }
169
 
170
- date_default_timezone_set($current);
171
 
172
- return $zones_array;
173
  }
174
 
175
  /**
@@ -178,18 +195,18 @@ function mailchimp_get_timezone_list() {
178
  */
179
  function activate_mailchimp_woocommerce()
180
  {
181
- // if we don't have woocommerce we need to display a horrible error message before the plugin is installed.
182
- if (!is_plugin_active('woocommerce/woocommerce.php')) {
183
- // Deactivate the plugin
184
- deactivate_plugins(__FILE__);
185
- $error_message = __('The MailChimp For WooCommerce plugin requires the <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> plugin to be active!', 'woocommerce');
186
- wp_die($error_message);
187
- }
188
-
189
- // ok we can activate this thing.
190
- require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce-activator.php';
191
-
192
- MailChimp_Woocommerce_Activator::activate();
193
  }
194
 
195
  /**
@@ -197,8 +214,8 @@ function activate_mailchimp_woocommerce()
197
  */
198
  function install_mailchimp_queue()
199
  {
200
- require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce-activator.php';
201
- MailChimp_Woocommerce_Activator::create_queue_tables();
202
  }
203
 
204
  /**
@@ -206,26 +223,26 @@ function install_mailchimp_queue()
206
  * This action is documented in includes/class-mailchimp-woocommerce-deactivator.php
207
  */
208
  function deactivate_mailchimp_woocommerce() {
209
- require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce-deactivator.php';
210
- MailChimp_Woocommerce_Deactivator::deactivate();
211
  }
212
 
213
  /**
214
  * See if we need to run any updates.
215
  */
216
  function run_mailchimp_plugin_updater() {
217
- if (!class_exists('PucFactory')) {
218
- require plugin_dir_path( __FILE__ ) . 'includes/plugin-update-checker/plugin-update-checker.php';
219
- }
220
-
221
- /** @var \PucGitHubChecker_3_1 $checker */
222
- $updater = PucFactory::getLatestClassVersion('PucGitHubChecker');
223
-
224
- if (class_exists($updater)) {
225
- $env = mailchimp_environment_variables();
226
- $checker = new $updater('https://github.com/mailchimp/mc-woocommerce/', __FILE__, $env->repo, 1);
227
- $checker->handleManualCheck();
228
- }
229
  }
230
 
231
  /**
@@ -236,29 +253,29 @@ function run_mailchimp_plugin_updater() {
236
  */
237
  function mailchimp_log($action, $message, $data = array())
238
  {
239
- $options = MailChimp_Woocommerce::getLoggingConfig();
240
-
241
- if (!$options->enable_logging || !$options->account_id || !$options->username) {
242
- return false;
243
- }
244
-
245
- $data = array(
246
- 'account_id' => $options->account_id,
247
- 'username' => $options->username,
248
- 'store_domain' => site_url(),
249
- 'platform' => 'woocommerce',
250
- 'action' => $action,
251
- 'message' => $message,
252
- 'data' => $data,
253
- );
254
-
255
- return wp_remote_post($options->endpoint, array(
256
- 'headers' => array(
257
- 'Accept: application/json',
258
- 'Content-Type: application/json'
259
- ),
260
- 'body' => json_encode($data),
261
- ));
262
  }
263
 
264
  /**
@@ -270,13 +287,13 @@ function mailchimp_log($action, $message, $data = array())
270
  */
271
  function mailchimp_string_contains($haystack, $needles)
272
  {
273
- foreach ((array) $needles as $needle) {
274
- if ($needle != '' && mb_strpos($haystack, $needle) !== false) {
275
- return true;
276
- }
277
- }
278
 
279
- return false;
280
  }
281
 
282
 
@@ -284,25 +301,25 @@ function mailchimp_string_contains($haystack, $needles)
284
  * @return int
285
  */
286
  function mailchimp_get_product_count() {
287
- $posts = mailchimp_count_posts('product');
288
- $total = 0;
289
- foreach ($posts as $status => $count) {
290
- $total += $count;
291
- }
292
- return $total;
293
  }
294
 
295
  /**
296
  * @return int
297
  */
298
  function mailchimp_get_order_count() {
299
- $posts = mailchimp_count_posts('shop_order');
300
- unset($posts['auto-draft']);
301
- $total = 0;
302
- foreach ($posts as $status => $count) {
303
- $total += $count;
304
- }
305
- return $total;
306
  }
307
 
308
  /**
@@ -310,14 +327,14 @@ function mailchimp_get_order_count() {
310
  * @return array|null|object
311
  */
312
  function mailchimp_count_posts($type) {
313
- global $wpdb;
314
- $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s GROUP BY post_status";
315
- $posts = $wpdb->get_results( $wpdb->prepare($query, $type));
316
- $response = array();
317
- foreach ($posts as $post) {
318
- $response[$post->post_status] = $post->num_posts;
319
- }
320
- return $response;
321
  }
322
 
323
  register_activation_hook( __FILE__, 'activate_mailchimp_woocommerce' );
@@ -330,6 +347,7 @@ register_deactivation_hook( __FILE__, 'deactivate_mailchimp_woocommerce' );
330
  require plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce.php';
331
 
332
  /**
 
333
  * Begins execution of the plugin.
334
  *
335
  * Since everything within the plugin is registered via hooks,
@@ -339,14 +357,14 @@ require plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce.php'
339
  * @since 1.0.0
340
  */
341
  function run_mailchimp_woocommerce() {
342
- $env = mailchimp_environment_variables();
343
- $plugin = new MailChimp_Woocommerce($env->environment, $env->version);
344
- $plugin->run();
345
  }
346
 
347
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
348
- $forwarded_address = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
349
- $_SERVER['REMOTE_ADDR'] = $forwarded_address[0];
350
  }
351
 
352
  function mailchimp_woocommerce_add_meta_tags() {
@@ -361,4 +379,3 @@ add_action('admin_init', 'run_mailchimp_plugin_updater');
361
  /** Add all the MailChimp hooks. */
362
  run_mailchimp_woocommerce();
363
 
364
-
16
  * Plugin Name: MailChimp for WooCommerce
17
  * Plugin URI: https://mailchimp.com/connect-your-store/
18
  * Description: MailChimp - WooCommerce plugin
19
+ * Version: 1.1.1
20
  * Author: MailChimp
21
  * Author URI: https://mailchimp.com
22
  * License: GPL-2.0+
27
 
28
  // If this file is called directly, abort.
29
  if ( ! defined( 'WPINC' ) ) {
30
+ die;
31
  }
32
 
33
  /**
34
  * @return object
35
  */
36
  function mailchimp_environment_variables() {
37
+ global $wp_version;
38
+
39
+ return (object) array(
40
+ 'repo' => 'master',
41
+ 'environment' => 'production',
42
+ 'version' => '1.1.1',
43
+ 'wp_version' => (empty($wp_version) ? 'Unknown' : $wp_version),
44
+ );
45
  }
46
 
47
  /**
48
  * @return bool|int
49
  */
50
  function mailchimp_get_list_id() {
51
+ if (($options = get_option('mailchimp-woocommerce', false)) && is_array($options)) {
52
+ if (isset($options['mailchimp_list'])) {
53
+ return $options['mailchimp_list'];
54
+ }
55
+ }
56
+ return false;
57
  }
58
 
59
  /**
60
  * @return string
61
  */
62
  function mailchimp_get_store_id() {
63
+ $store_id = mailchimp_get_data('store_id', false);
64
+ if (empty($store_id)) {
65
+ // this is for the previous installs that had been applying the MC store id as the siteurl.
66
+ // patched to the random hash because people were changing this value for various reasons.
67
+ $store_id = md5(get_option('siteurl'));
68
+ mailchimp_set_data('store_id', $store_id, 'yes');
69
+ }
70
+ return $store_id;
71
  }
72
 
73
  /**
74
  * @return bool|MailChimp_WooCommerce_MailChimpApi
75
  */
76
  function mailchimp_get_api() {
77
+ if (($options = get_option('mailchimp-woocommerce', false)) && is_array($options)) {
78
+ if (isset($options['mailchimp_api_key'])) {
79
+ return new MailChimp_WooCommerce_MailChimpApi($options['mailchimp_api_key']);
80
+ }
81
+ }
82
+ return false;
83
  }
84
 
85
  /**
88
  * @return null
89
  */
90
  function mailchimp_get_option($key, $default = null) {
91
+ $options = get_option('mailchimp-woocommerce');
92
+ if (!is_array($options)) {
93
+ return $default;
94
+ }
95
+ if (!array_key_exists($key, $options)) {
96
+ return $default;
97
+ }
98
+ return $options[$key];
99
  }
100
 
101
  /**
102
  * @param $key
103
+ * @param null $default
104
+ * @return mixed
105
  */
106
+ function mailchimp_get_data($key, $default = null) {
107
+ return get_option('mailchimp-woocommerce-'.$key, $default);
108
+ }
109
+
110
+ /**
111
+ * @param $key
112
+ * @param $value
113
+ * @param string $autoload
114
+ * @return bool
115
+ */
116
+ function mailchimp_set_data($key, $value, $autoload = 'yes') {
117
+ return update_option('mailchimp-woocommerce-'.$key, $value, $autoload);
118
  }
119
 
120
  /**
122
  * @return DateTime
123
  */
124
  function mailchimp_date_utc($date) {
125
+ $timezone = wc_timezone_string();
126
+ //$timezone = mailchimp_get_option('store_timezone', 'America/New_York');
127
+ if (is_numeric($date)) {
128
+ $stamp = $date;
129
+ $date = new \DateTime('now', new DateTimeZone($timezone));
130
+ $date->setTimestamp($stamp);
131
+ } else {
132
+ $date = new \DateTime($date, new DateTimeZone($timezone));
133
+ }
134
+
135
+ $date->setTimezone(new DateTimeZone('UTC'));
136
+ return $date;
137
  }
138
 
139
  /**
142
  */
143
  function mailchimp_date_local($date) {
144
  $timezone = mailchimp_get_option('store_timezone', 'America/New_York');
145
+ if (is_numeric($date)) {
146
+ $stamp = $date;
147
+ $date = new \DateTime('now', new DateTimeZone('UTC'));
148
+ $date->setTimestamp($stamp);
149
+ } else {
150
+ $date = new \DateTime($date, new DateTimeZone('UTC'));
151
+ }
152
 
153
  $date->setTimezone(new DateTimeZone($timezone));
154
  return $date;
159
  * @return mixed
160
  */
161
  function mailchimp_array_remove_empty($data) {
162
+ if (empty($data) || !is_array($data)) {
163
+ return array();
164
+ }
165
+ foreach ($data as $key => $value) {
166
+ if ($value === null || $value === '') {
167
+ unset($data[$key]);
168
+ }
169
+ }
170
+ return $data;
171
  }
172
 
173
  /**
174
  * @return array
175
  */
176
  function mailchimp_get_timezone_list() {
177
+ $zones_array = array();
178
+ $timestamp = time();
179
+ $current = date_default_timezone_get();
180
 
181
+ foreach(timezone_identifiers_list() as $key => $zone) {
182
+ date_default_timezone_set($zone);
183
+ $zones_array[$key]['zone'] = $zone;
184
+ $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
185
+ }
186
 
187
+ date_default_timezone_set($current);
188
 
189
+ return $zones_array;
190
  }
191
 
192
  /**
195
  */
196
  function activate_mailchimp_woocommerce()
197
  {
198
+ // if we don't have woocommerce we need to display a horrible error message before the plugin is installed.
199
+ if (!is_plugin_active('woocommerce/woocommerce.php')) {
200
+ // Deactivate the plugin
201
+ deactivate_plugins(__FILE__);
202
+ $error_message = __('The MailChimp For WooCommerce plugin requires the <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> plugin to be active!', 'woocommerce');
203
+ wp_die($error_message);
204
+ }
205
+
206
+ // ok we can activate this thing.
207
+ require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce-activator.php';
208
+
209
+ MailChimp_Woocommerce_Activator::activate();
210
  }
211
 
212
  /**
214
  */
215
  function install_mailchimp_queue()
216
  {
217
+ require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce-activator.php';
218
+ MailChimp_Woocommerce_Activator::create_queue_tables();
219
  }
220
 
221
  /**
223
  * This action is documented in includes/class-mailchimp-woocommerce-deactivator.php
224
  */
225
  function deactivate_mailchimp_woocommerce() {
226
+ require_once plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce-deactivator.php';
227
+ MailChimp_Woocommerce_Deactivator::deactivate();
228
  }
229
 
230
  /**
231
  * See if we need to run any updates.
232
  */
233
  function run_mailchimp_plugin_updater() {
234
+ if (!class_exists('PucFactory')) {
235
+ require plugin_dir_path( __FILE__ ) . 'includes/plugin-update-checker/plugin-update-checker.php';
236
+ }
237
+
238
+ /** @var \PucGitHubChecker_3_1 $checker */
239
+ $updater = PucFactory::getLatestClassVersion('PucGitHubChecker');
240
+
241
+ if (class_exists($updater)) {
242
+ $env = mailchimp_environment_variables();
243
+ $checker = new $updater('https://github.com/mailchimp/mc-woocommerce/', __FILE__, $env->repo, 1);
244
+ $checker->handleManualCheck();
245
+ }
246
  }
247
 
248
  /**
253
  */
254
  function mailchimp_log($action, $message, $data = array())
255
  {
256
+ $options = MailChimp_Woocommerce::getLoggingConfig();
257
+
258
+ if (!$options->enable_logging || !$options->account_id || !$options->username) {
259
+ return false;
260
+ }
261
+
262
+ $data = array(
263
+ 'account_id' => $options->account_id,
264
+ 'username' => $options->username,
265
+ 'store_domain' => site_url(),
266
+ 'platform' => 'woocommerce',
267
+ 'action' => $action,
268
+ 'message' => $message,
269
+ 'data' => $data,
270
+ );
271
+
272
+ return wp_remote_post($options->endpoint, array(
273
+ 'headers' => array(
274
+ 'Accept: application/json',
275
+ 'Content-Type: application/json'
276
+ ),
277
+ 'body' => json_encode($data),
278
+ ));
279
  }
280
 
281
  /**
287
  */
288
  function mailchimp_string_contains($haystack, $needles)
289
  {
290
+ foreach ((array) $needles as $needle) {
291
+ if ($needle != '' && mb_strpos($haystack, $needle) !== false) {
292
+ return true;
293
+ }
294
+ }
295
 
296
+ return false;
297
  }
298
 
299
 
301
  * @return int
302
  */
303
  function mailchimp_get_product_count() {
304
+ $posts = mailchimp_count_posts('product');
305
+ $total = 0;
306
+ foreach ($posts as $status => $count) {
307
+ $total += $count;
308
+ }
309
+ return $total;
310
  }
311
 
312
  /**
313
  * @return int
314
  */
315
  function mailchimp_get_order_count() {
316
+ $posts = mailchimp_count_posts('shop_order');
317
+ unset($posts['auto-draft']);
318
+ $total = 0;
319
+ foreach ($posts as $status => $count) {
320
+ $total += $count;
321
+ }
322
+ return $total;
323
  }
324
 
325
  /**
327
  * @return array|null|object
328
  */
329
  function mailchimp_count_posts($type) {
330
+ global $wpdb;
331
+ $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s GROUP BY post_status";
332
+ $posts = $wpdb->get_results( $wpdb->prepare($query, $type));
333
+ $response = array();
334
+ foreach ($posts as $post) {
335
+ $response[$post->post_status] = $post->num_posts;
336
+ }
337
+ return $response;
338
  }
339
 
340
  register_activation_hook( __FILE__, 'activate_mailchimp_woocommerce' );
347
  require plugin_dir_path( __FILE__ ) . 'includes/class-mailchimp-woocommerce.php';
348
 
349
  /**
350
+ *
351
  * Begins execution of the plugin.
352
  *
353
  * Since everything within the plugin is registered via hooks,
357
  * @since 1.0.0
358
  */
359
  function run_mailchimp_woocommerce() {
360
+ $env = mailchimp_environment_variables();
361
+ $plugin = new MailChimp_Woocommerce($env->environment, $env->version);
362
+ $plugin->run();
363
  }
364
 
365
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
366
+ $forwarded_address = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
367
+ $_SERVER['REMOTE_ADDR'] = $forwarded_address[0];
368
  }
369
 
370
  function mailchimp_woocommerce_add_meta_tags() {
379
  /** Add all the MailChimp hooks. */
380
  run_mailchimp_woocommerce();
381