WooCommerce MailChimp - Version 2.0

Version Description

Download this release

Release Info

Developer anderly
Plugin Icon 128x128 WooCommerce MailChimp
Version 2.0
Comparing to
See all releases

Code changes from version 1.3.9 to 2.0

COPYRIGHT.txt CHANGED
@@ -1,28 +1,28 @@
1
-
2
- WooCommerce MailChimp
3
-
4
- Copyright 2015 "anderly" (Adam Anderly) www.anderly.com
5
-
6
- The files COPYRIGHT.txt and LICENSE.txt as well as ALL NOTICES IN THE
7
- HEADERS OF ALL FILES MUST BE KEPT INTACT.
8
-
9
- GPL-licensed
10
-
11
- Unless otherwise stated, all code in this plugin is licensed under
12
- the GPL License:
13
-
14
- All code is part of this plugin, hereinafter referred to as "program".
15
-
16
- This program is free software: you can redistribute it and/or modify
17
- it under the terms of the GNU General Public License as published by
18
- the Free Software Foundation, either version 3 of the License, or
19
- (at your option) any later version.
20
-
21
- This program is distributed in the hope that it will be useful,
22
- but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
- GNU General Public License for more details.
25
-
26
- You should have received a copy of the GNU General Public License
27
- along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 
1
+
2
+ WooCommerce MailChimp
3
+
4
+ Copyright 2016 "saintsystems" (Saint Systems) www.saintsystems.com
5
+
6
+ The files COPYRIGHT.txt and LICENSE.txt as well as ALL NOTICES IN THE
7
+ HEADERS OF ALL FILES MUST BE KEPT INTACT.
8
+
9
+ GPL-licensed
10
+
11
+ Unless otherwise stated, all code in this plugin is licensed under
12
+ the GPL License:
13
+
14
+ All code is part of this plugin, hereinafter referred to as "program".
15
+
16
+ This program is free software: you can redistribute it and/or modify
17
+ it under the terms of the GNU General Public License as published by
18
+ the Free Software Foundation, either version 3 of the License, or
19
+ (at your option) any later version.
20
+
21
+ This program is distributed in the hope that it will be useful,
22
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ GNU General Public License for more details.
25
+
26
+ You should have received a copy of the GNU General Public License
27
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 
README.md DELETED
@@ -1,161 +0,0 @@
1
- ## WooCommerce MailChimp ##
2
- [![WordPress tested](http://img.shields.io/wordpress/v/woocommerce-mailchimp.svg?style=flat-square)](https://wordpress.org/plugins/woocommerce-mailchimp/)
3
- [![WordPress Plugin version](http://img.shields.io/wordpress/plugin/v/woocommerce-mailchimp.svg?style=flat-square)](https://wordpress.org/plugins/woocommerce-mailchimp/)
4
- [![WordPress Plugin Downloads](http://img.shields.io/wordpress/plugin/dt/woocommerce-mailchimp.svg?style=flat-square)](https://wordpress.org/plugins/woocommerce-mailchimp/)
5
- [![WordPress Plugin Rating](http://img.shields.io/wordpress/plugin/r/woocommerce-mailchimp.svg?style=flat-square)](https://wordpress.org/plugins/woocommerce-mailchimp/)
6
- [![License](http://img.shields.io/badge/license-GPLv3-red.svg?style=flat-square)](http://opensource.org/licenses/GPL-3.0)
7
-
8
- WooCommerce MailChimp provides simple and flexible MailChimp integration for WooCommerce.
9
-
10
- Automatically subscribe customers to a designated MailChimp list and, optionally, MailChimp interest groups upon order creation or order completion. This can be done quietly or based on the user's consent with several opt-in settings that support international opt-in laws.
11
-
12
- ### Features
13
-
14
- #### WooCommerce Event Selection
15
-
16
- - Subscribe customers to MailChimp after order creation
17
- - Subscribe customers to MailChimp after order completion
18
-
19
- #### Works with MailChimp Interest Groups
20
-
21
- - Set one or more interest groups to add users to based on the selected MailChimp list. The title of the groups needs to be entered as the Group Name.
22
-
23
- #### Opt-In Settings
24
-
25
- - MailChimp double opt-in support (control whether a double opt-in email is sent to the customer)
26
- - Optionally, display an opt-in checkbox on the checkout page (this is required in some countries)
27
- - Control the label displayed next to the opt-in checkbox
28
- - Control whether or not the opt-in checkbox is checked or unchecked by default
29
- - Control the placement of the opt-in checkbox on the checkout page
30
-
31
- #### Translation Support
32
-
33
- - i18n ready with included woocommerce-mailchimp.pot file
34
- - WPML support via wpml-config.xml
35
-
36
- **Included Translations:**
37
-
38
- - English (default)
39
- - No other translations yet.
40
-
41
- Thanks in advance for your help on any translation efforts!
42
-
43
- #### Multisite
44
-
45
- - All features should work for each blog in multisite installations.
46
-
47
- ### Feedback
48
-
49
- Feedback is welcome!
50
-
51
- If you need help, have problems, want to leave feedback or want to provide constructive criticism, please do so here at the [WooCommerce MailChimp plugin page](http://anderly.com/woocommerce-mailchimp/).
52
-
53
- #### Twitter
54
-
55
- [Follow @anderly on Twitter](http://twitter.com/anderly) for updates on this and other plugins.
56
-
57
- ### Installation
58
-
59
- 1. Upload or extract the `woocommerce-mailchimp` folder to your site's `/wp-content/plugins/` directory. You can also use the *Add new* option found in the *Plugins* menu in WordPress.
60
- 2. Enable the plugin from the *Plugins* menu in WordPress.
61
-
62
- ### Usage
63
-
64
- 1. Go to WooCommerce > Settings > Integration > MailChimp
65
- 2. First, enable the plugin and set your MailChimp API Key and hit save.
66
- 3. Select whether you want customers to be subscribed to your MailChimp list after order creation or order completion (there's a difference in WooCommerce).
67
- 4. Next, select your MailChimp list and set any interest group settings (optional) and hit save.
68
- 5. That's it, now customers who purchase products from your WooCommerce store will automatically be subscribed to the selected list (and optional interest groups) in MailChimp!
69
-
70
- ### Changelog
71
-
72
- #### 1.3.9
73
- * Rename MCAPI class to prevent collisions with other plugins using MCAPI
74
-
75
- #### 1.3.8
76
- * WordPress 4.6.x Compatible
77
- * WooCommerce 2.6.x Compatible
78
- * More flexible opt_in checkbox placement
79
- * Pass $order_id to `ss_wc_mailchimp_subscribe_options` hook
80
- * Use only one instance of MCAPI
81
- * Fixed Issue #14 MCAPI constructor style
82
- * Fixed Issue #15 `mailchimp_api_error_msg`
83
- * Fixed Issue #16 where lists wouldn't show up until you saved the settings twice
84
-
85
- #### 1.3.7
86
- * WordPress 4.4 Compatible
87
- * WooCommerce 2.4.12 Compatible
88
- * API response not shown in debug log
89
- * Use only one instance of MCAPI
90
-
91
- #### 1.3.6
92
- * Backout of change to use WC_Logger due to fatal error
93
-
94
- #### 1.3.5
95
- * Fix for undefined variable list and array_merge issue.
96
- * Change to use WC_Logger instead of error_log
97
- * Updated pot file
98
- * Added French translation
99
- * General code syntax cleanup
100
-
101
- #### 1.3.4
102
- * Fix enabled check. Issue #6.
103
-
104
- #### 1.3.3
105
- * Fix for transient key length. Tested with WordPress 4.1
106
-
107
- #### 1.3.2
108
- * Fix for headers already sent message. Tested with WordPress 4.0 and WooCommerce 2.2.*
109
-
110
- #### 1.3.1
111
- * Fix for MailChimp merge vars bug introduced in v1.3
112
-
113
- #### 1.3
114
- * Breaking Change: 'ss_wc_mailchimp_subscribe_merge_vars' action filter now passes $order_id param to enable retrieving additional order info/meta to use in MailChimp merge vars
115
- * Small fix to order_created subscribe event to work with PayPal Payment Gateway
116
-
117
- #### 1.2.6
118
- * Added additional debug logging when WP_DEBUG is enabled
119
-
120
- #### 1.2.5
121
- * Bug fix for subscribe when not using opt-in display field
122
-
123
- ##### 1.2.4
124
- * Bug fix for subscribe
125
-
126
- ##### 1.2.3
127
- * Fixed plugin settings link for WooCommerce 2.1
128
-
129
- ##### 1.2.2
130
- * WooCommerce 2.1 fix for order custom fields
131
-
132
- ##### 1.2.1
133
- * WooCommerce 2.1 integration: Change to use wc_enqueue_js instead of add_inline_js
134
- * WooCommerce 2.1 integration: Change to support new default checkout field filter for default opt-in checkbox status
135
-
136
- ##### 1.2
137
- * Added new setting to control whether or not the double opt-in checkbox is checked/unchecked by default on the checkout page.
138
- * Added new setting to control display location of the double opt-in checkbox (under billing info or order info)
139
- * Small modification to append to MailChimp interest groups for existing users so that group settings are not lost for users who were already subscribed.
140
- * Preparations for i18n (Internationalization) support. Several users have already asked and offered to translate the plugin into other languages. We had always planned on that, but now are making that a reality.
141
-
142
- ##### 1.1.3
143
- * Minor action hook change since order meta (needed for MailChimp API call) is not yet available on 'woocommerce_new_order' hook
144
-
145
- ##### 1.1.2
146
- * Update to REALLY address issue with subscriptions not occurring on order create "pending"
147
-
148
- ##### 1.1.1
149
- * Update to address issue with subscriptions not occurring on order create "pending"
150
-
151
- ##### 1.1
152
- * Add the option to display an opt-in field on checkout
153
-
154
- ##### 1.0.2
155
- * Minor text and comment changes
156
-
157
- ##### 1.0.1
158
- * Added "Settings" link on the Plugins administration screen
159
-
160
- ##### 1.0
161
- * This is the first public release.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
assets/css/style.css ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .loading-indicator {
2
+ display: inline-block;
3
+ vertical-align:top;
4
+ margin-top:5px;
5
+ }
6
+
7
+ .woocommerce table.form-table .select2-container {
8
+ display: inline-block !important;
9
+ }
10
+
11
+ .woocommerce-mailchimp-loading {
12
+ margin-left: 5px;
13
+ margin-top: 2px;
14
+ display: inline-block;
15
+ }
16
+
17
+ .woocommerce-mailchimp-loading-indicator {
18
+ background: url(../images/loading.gif) center left no-repeat;
19
+ background-size: 16px 16px;
20
+ line-height: 16px;
21
+ padding-right: 16px;
22
+ margin-right: 5px;
23
+ }
assets/images/loading.gif ADDED
Binary file
assets/js/woocommerce-mailchimp-admin.js ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * WooCommerce MailChimp Plugin
3
+ */
4
+ var SS_WC_MailChimp = function($) {
5
+
6
+ var $enabled;
7
+ var $apiKey;
8
+ var $listsLoadingIndicator;
9
+ var $mainList;
10
+ var $interestGroupsLoadingIndicator;
11
+ var $interestGroups;
12
+ var $displayOptIn;
13
+ var $occurs;
14
+ var $optInLabel;
15
+ var $optInCheckboxDefault;
16
+ var $optInCheckboxLocation;
17
+ var $doubleOptIn;
18
+
19
+ var namespace = 'ss_wc_mailchimp';
20
+
21
+ return {
22
+ init: init,
23
+ loadLists: loadLists,
24
+ loadGroups: loadGroups,
25
+ };
26
+
27
+ function init() {
28
+
29
+ initHandles();
30
+ initHandlers();
31
+ initLists();
32
+ initGroups();
33
+
34
+ } //end function init
35
+
36
+ function initHandles() {
37
+ // Capture jQuery handles to elements
38
+ $enabled = $('#' + namespace_prefixed('enabled'));
39
+ $apiKey = $('#' + namespace_prefixed('api_key'));
40
+ $mainList = $('#' + namespace_prefixed('list'));
41
+ $interestGroups = $('#' + namespace_prefixed('interest_groups'));
42
+ $displayOptIn = $('#' + namespace_prefixed('display_opt_in'));
43
+ $occurs = $('#' + namespace_prefixed('occurs'));
44
+ $optInLabel = $('#' + namespace_prefixed('opt_in_label'));
45
+ $optInCheckboxDefault = $('#' + namespace_prefixed('opt_in_checkbox_default_status'));
46
+ $optInCheckboxLocation = $('#' + namespace_prefixed('opt_in_checkbox_display_location'));
47
+ $doubleOptIn = $('#' + namespace_prefixed('double_opt_in'));
48
+ }
49
+
50
+ function initHandlers() {
51
+ //$apiKey.closest('tr').hide();
52
+ $mainList.closest('tr').hide();
53
+
54
+ if ($mainList.val() === '') {
55
+ $interestGroups.attr('disabled','disabled');
56
+ }
57
+ $apiKey.change(function() {
58
+ if ( $apiKey.val() === '' ) {
59
+ toggleAllSettings('hide');
60
+ } else {
61
+ toggleAllSettings('show');
62
+ }
63
+ }).change();
64
+
65
+ $mainList.change(function() {
66
+ if ($mainList.val()) {
67
+ $interestGroups.removeAttr('disabled');
68
+ } else {
69
+ $interestGroups.children().remove();
70
+ $interestGroups.attr('disabled','disabled');
71
+ }
72
+ });
73
+
74
+ $apiKey.on('blur', function(e) {
75
+ if ($apiKey.val() === '') {
76
+ loadLists($apiKey.val());
77
+ }
78
+ }).change();
79
+
80
+ $apiKey.on('paste cut', function() {
81
+
82
+ // Short pause to wait for paste to complete
83
+ setTimeout( function() {
84
+ loadLists($apiKey.val());
85
+ $apiKey.change();
86
+ }, 100);
87
+ });
88
+
89
+ $mainList.on('change', function() {
90
+ if ($mainList.val() !== '') {
91
+ loadGroups($apiKey.val(), $mainList.val());
92
+ }
93
+ });
94
+
95
+ $optInLabel.closest('tr').hide();
96
+ $optInCheckboxDefault.closest('tr').hide();
97
+ $optInCheckboxLocation.closest('tr').hide();
98
+ $doubleOptIn.closest('tr').hide();
99
+ $displayOptIn.change(function() {
100
+ switch ($displayOptIn.val()) {
101
+ // case '0':
102
+ // $occurs.closest('tr').fadeOut();
103
+ // $optInLabel.closest('tr').fadeOut();
104
+ // $optInCheckboxDefault.closest('tr').fadeOut();
105
+ // $optInCheckboxLocation.closest('tr').fadeOut();
106
+ // $doubleOptIn.closest('tr').fadeOut();
107
+ // break;
108
+ case 'no':
109
+ $optInLabel.closest('tr').fadeOut();
110
+ $optInCheckboxDefault.closest('tr').fadeOut();
111
+ $optInCheckboxLocation.closest('tr').fadeOut();
112
+ $doubleOptIn.closest('tr').fadeIn();
113
+ break;
114
+ case 'yes':
115
+ $optInLabel.closest('tr').fadeIn();
116
+ $optInCheckboxDefault.closest('tr').fadeIn();
117
+ $optInCheckboxLocation.closest('tr').fadeIn();
118
+ $doubleOptIn.closest('tr').fadeIn();
119
+ break;
120
+ }
121
+ }).change();
122
+
123
+ } //end function initHandlers
124
+
125
+ function initLists() {
126
+ $listsLoadingIndicator = $('<div id="ss_wc_mailchimp_loading_lists" class="woocommerce-mailchimp-loading"><span class="woocommerce-mailchimp-loading-indicator"></span>'+SS_WC_MailChimp_Messages.connecting_to_mailchimp+'</div>');
127
+ $mainList.after($listsLoadingIndicator.hide());
128
+
129
+ } //end function initLists
130
+
131
+ function initGroups() {
132
+ //return;
133
+
134
+ // Reinitialize the <optgroup> elements by splitting out the option names
135
+ var currentGroup = '';
136
+ var lastGroup = '';
137
+ var grouping;
138
+ var $options = $interestGroups.children('option').clone();
139
+ $interestGroups.attr('data-placeholder', SS_WC_MailChimp_Messages.select_groups_placeholder);
140
+ // $interestGroups.children().remove();
141
+ // for (i = 0; i < $options.length; i++) {
142
+ // item = $options[i];
143
+ // currentGroup = item.text.split(':')[0];
144
+ // if (currentGroup !== lastGroup) {
145
+ // grouping = $('<optgroup>').attr('label', currentGroup);
146
+ // $interestGroups.append(grouping);
147
+ // }
148
+ // item.text = item.text.split(':')[1];
149
+ // grouping.append(item);
150
+ // lastGroup = currentGroup;
151
+ // }
152
+ $interestGroups.select2('destroy').select2();
153
+ var groupsMessage = $('#ss-wc-mailchimp-groups-msg').length > 0 ? $('#ss-wc-mailchimp-groups-msg') : $('<div id="ss-wc-mailchimp-groups-msg" style="display: inline-block"/>');
154
+ $interestGroups.after(groupsMessage);
155
+ if ($options.length === 0) {
156
+ groupsMessage.text(SS_WC_MailChimp_Messages.interest_groups_not_enabled);
157
+ $interestGroups.siblings('.select2-container').remove();
158
+ groupsMessage.show();
159
+ } else {
160
+ $interestGroups.siblings('.select2-container').show();
161
+ groupsMessage.hide();
162
+ }
163
+
164
+ // Add the loading indicator for groups (set to hidden by default)
165
+ $interestGroupsLoadingIndicator = $('<div id="ss_wc_mailchimp_loading_groups" class="woocommerce-mailchimp-loading"><span class="woocommerce-mailchimp-loading-indicator"></span>'+SS_WC_MailChimp_Messages.connecting_to_mailchimp+'</div>');
166
+ $interestGroups.parent().append($interestGroupsLoadingIndicator.hide());
167
+
168
+ } //end function initGroups
169
+
170
+ function loadLists(apiKey) {
171
+
172
+ /**
173
+ * Load lists
174
+ */
175
+ $mainList.attr('disabled','disabled');
176
+ $listsLoadingIndicator.show();
177
+ $.post(
178
+ ajaxurl,
179
+ {
180
+ 'action': '' + namespace_prefixed('get_lists'),
181
+ 'data': { 'api_key': apiKey }
182
+ },
183
+ function(response) {
184
+ console.log(response);
185
+ $listsLoadingIndicator.hide();
186
+ var result = [];
187
+
188
+ try {
189
+ result = $.parseJSON(response);
190
+ } catch (err) {
191
+ console.error(err);
192
+ alert(SS_WC_MailChimp_Messages.error_loading_lists);
193
+ }
194
+
195
+ if (result) {
196
+ $mainList.select2('destroy');
197
+ $mainList.removeAttr('disabled');
198
+ $mainList.children().remove();
199
+ $.each(result, function(key, val) {
200
+ $mainList
201
+ .append($('<option></option>')
202
+ .attr('value',key)
203
+ .text(val));
204
+ });
205
+ $mainList.select2();
206
+ }
207
+ }
208
+ );
209
+
210
+ } //end function loadLists
211
+
212
+ function loadGroups(apiKey, listId) {
213
+
214
+ /**
215
+ * Load interest groups
216
+ */
217
+ $interestGroups.attr('disabled','disabled');
218
+
219
+ $interestGroups.children().remove();
220
+
221
+ $interestGroups.select2().hide();
222
+ $('#ss-wc-mailchimp-groups-msg').hide();
223
+ $interestGroupsLoadingIndicator.show();
224
+ $.post(
225
+ ajaxurl,
226
+ {
227
+ 'action': '' + namespace_prefixed('get_interest_groups'),
228
+ 'data': { 'api_key': apiKey, 'list_id': listId }
229
+ },
230
+ function(response) {
231
+ console.log(response);
232
+ $interestGroupsLoadingIndicator.hide();
233
+ var result = [];
234
+
235
+ try {
236
+ result = $.parseJSON(response);
237
+ } catch (err) {
238
+ console.error(err);
239
+ alert(SS_WC_MailChimp_Messages.error_loading_groups);
240
+ }
241
+
242
+ if (result.error) {
243
+ $('#ss-wc-mailchimp-groups-msg').text(result.error).show();
244
+ $interestGroups.children().remove();
245
+ $interestGroups.select2('destroy');
246
+ $interestGroups.hide();
247
+ return;
248
+ }
249
+
250
+ if (result.length === 0) {
251
+ $('#ss-wc-mailchimp-groups-msg').show();
252
+ initGroups();
253
+ return;
254
+ }
255
+
256
+ $interestGroups.show();
257
+ $interestGroups.removeAttr('disabled');
258
+ $interestGroups.children().remove();
259
+ $.each(result, function(id, grouping) {
260
+ $interestGroups.append(
261
+ $('<option></option>')
262
+ .attr('value',id)
263
+ .text(grouping));
264
+ });
265
+
266
+ initGroups();
267
+ //$interestGroups.select2('destroy').select2();
268
+ }
269
+ );
270
+
271
+ } //end function loadGroups
272
+
273
+ function toggleAllSettings( show_hide ) {
274
+ if (show_hide == 'show') {
275
+ $apiKey.closest('tr').nextAll('tr').fadeIn();
276
+ $apiKey.closest('table').nextAll('h2, .form-table').fadeIn();
277
+ } else {
278
+ $apiKey.closest('tr').nextAll('tr').fadeOut();
279
+ $apiKey.closest('table').nextAll('h2, .form-table').fadeOut();
280
+ }
281
+ }
282
+
283
+ function namespace_prefixed( suffix ) {
284
+ return namespace + '_' + suffix;
285
+ }
286
+
287
+ }(jQuery);
config/default-settings.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ return array(
3
+ 'api_key' => '',
4
+ 'enabled' => 'yes',
5
+ 'list' => '',
6
+ 'interest_groups' => '',
7
+ 'display_opt_in' => 'no',
8
+ 'occurs' => 'pending',
9
+ 'double_opt_in' => 'no',
10
+ 'opt_in_label' => __( 'Subscribe to our newsletter', 'ss_wc_mailchimp' ),
11
+ 'opt_in_checkbox_default_status' => 'checked',
12
+ 'opt_in_checkbox_display_location' => 'woocommerce_review_order_before_submit',
13
+ 'debug' => 'no',
14
+ );
includes/class-ss-wc-integration-mailchimp.php ADDED
@@ -0,0 +1,766 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
+
5
+ /**
6
+ * MailChimp Integration
7
+ *
8
+ * Allows integration with MailChimp
9
+ *
10
+ * @class SS_WC_Integration_MailChimp
11
+ * @extends WC_Integration
12
+ * @version 1.4.0
13
+ * @package WooCommerce MailChimp
14
+ * @author Saint Systems
15
+ */
16
+ class SS_WC_Integration_MailChimp extends WC_Integration {
17
+
18
+ /**
19
+ * Instance of the API class.
20
+ * @var Object
21
+ */
22
+ private static $api_instance = null;
23
+
24
+ private $api_key = '';
25
+ private $debug = false;
26
+
27
+ /**
28
+ * Init and hook in the integration.
29
+ *
30
+ * @access public
31
+ * @return void
32
+ */
33
+ public function __construct() {
34
+
35
+ $this->id = 'mailchimp';
36
+ $this->method_title = __( 'MailChimp', 'ss_wc_mailchimp' );
37
+ $this->method_description = __( 'MailChimp is a popular email marketing service.', 'ss_wc_mailchimp' );
38
+
39
+ // Load the settings.
40
+ $this->init_settings();
41
+
42
+ if ( is_admin() && ! is_ajax() ) {
43
+ // Load the settings
44
+ $this->init_form_fields();
45
+ }
46
+
47
+ // Hooks
48
+ add_action( 'admin_notices', array( $this, 'checks' ) );
49
+
50
+ // Update the settings fields
51
+ add_action( 'woocommerce_update_options_integration', array( $this, 'process_admin_options') );
52
+
53
+ // Update the settings fields
54
+ add_action( 'woocommerce_update_options_integration', array( $this, 'refresh_settings'), 10 );
55
+
56
+ // Refresh the settings
57
+ add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'refresh_settings'), 10 );
58
+
59
+ // We would use the 'woocommerce_new_order' action but first name, last name and email address (order meta) is not yet available,
60
+ // so instead we use the 'woocommerce_checkout_update_order_meta' action hook which fires after the checkout process on the "thank you" page
61
+ add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'order_status_changed' ), 1000, 1 );
62
+
63
+ // hook into woocommerce order status changed hook to handle the desired subscription event trigger
64
+ add_action( 'woocommerce_order_status_changed', array( $this, 'order_status_changed' ), 10, 3 );
65
+
66
+ // Maybe add an "opt-in" field to the checkout
67
+ $opt_in_checkbox_display_location = !empty( $this->opt_in_checkbox_display_location() ) ? $this->opt_in_checkbox_display_location() : 'woocommerce_review_order_before_submit';
68
+
69
+ // Old opt-in checkbox display locations
70
+ $old_opt_in_checkbox_display_locations = array(
71
+ 'billing' => 'woocommerce_after_checkout_billing_form',
72
+ 'order' => 'woocommerce_review_order_before_submit',
73
+ );
74
+
75
+ // Map old billing/order checkbox display locations to new format
76
+ if ( array_key_exists( $opt_in_checkbox_display_location, $old_opt_in_checkbox_display_locations ) ) {
77
+ $opt_in_checkbox_display_location = $old_opt_in_checkbox_display_locations[ $opt_in_checkbox_display_location ];
78
+ }
79
+
80
+ add_action( $opt_in_checkbox_display_location, array( $this, 'maybe_add_checkout_fields' ) );
81
+ add_filter( 'default_checkout_ss_wc_mailchimp_opt_in', array( $this, 'checkbox_default_status' ) );
82
+
83
+ // Maybe save the "opt-in" field on the checkout
84
+ add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'maybe_save_checkout_fields' ) );
85
+ }
86
+
87
+ /**
88
+ * api_key function.
89
+ * @return string MailChimp API Key
90
+ */
91
+ public function api_key() {
92
+ return $this->get_option( 'api_key' );
93
+ }
94
+
95
+ /**
96
+ * is_enabled function.
97
+ *
98
+ * @access public
99
+ * @return boolean
100
+ */
101
+ public function is_enabled() {
102
+ return 'yes' === $this->get_option( 'enabled' );
103
+ }
104
+
105
+ /**
106
+ * occurs function
107
+ * @return string
108
+ */
109
+ public function occurs() {
110
+ return $this->get_option( 'occurs' );
111
+ }
112
+
113
+ /**
114
+ * list function.
115
+ *
116
+ * @access public
117
+ * @return string MailChimp list ID
118
+ */
119
+ public function list() {
120
+ return $this->get_option( 'list' );
121
+ }
122
+
123
+ /**
124
+ * double_optin function.
125
+ *
126
+ * @access public
127
+ * @return boolean
128
+ */
129
+ public function double_optin() {
130
+ return 'yes' === $this->get_option( 'double_optin' );
131
+ }
132
+
133
+ /**
134
+ * display_opt_in function.
135
+ *
136
+ * @access public
137
+ * @return boolean
138
+ */
139
+ public function display_opt_in() {
140
+ return 'yes' === $this->get_option( 'display_opt_in' );
141
+ }
142
+
143
+ /**
144
+ * opt_in_label function.
145
+ *
146
+ * @access public
147
+ * @return string
148
+ */
149
+ public function opt_in_label() {
150
+ return $this->get_option( 'opt_in_label' );
151
+ }
152
+
153
+ /**
154
+ * opt_in_checkbox_default_status function.
155
+ *
156
+ * @access public
157
+ * @return string
158
+ */
159
+ public function opt_in_checkbox_default_status() {
160
+ return $this->get_option( 'opt_in_checkbox_default_status' );
161
+ }
162
+
163
+ /**
164
+ * opt_in_checkbox_display_location function.
165
+ *
166
+ * @access public
167
+ * @return string
168
+ */
169
+ public function opt_in_checkbox_display_location() {
170
+ return $this->get_option( 'opt_in_checkbox_display_location' );
171
+ }
172
+
173
+ /**
174
+ * interests function.
175
+ *
176
+ * @access public
177
+ * @return array
178
+ */
179
+ public function interest_groups() {
180
+ return $this->get_option( 'interest_groups' );
181
+ }
182
+
183
+ /**
184
+ * has_list function.
185
+ *
186
+ * @access public
187
+ * @return boolean
188
+ */
189
+ public function has_list() {
190
+ if ( $this->list() ) {
191
+ return true;
192
+ }
193
+ return false;
194
+ }
195
+
196
+ /**
197
+ * has_api_key function.
198
+ *
199
+ * @access public
200
+ * @return boolean
201
+ */
202
+ public function has_api_key() {
203
+ return !empty( $this->api_key() );
204
+ }
205
+
206
+ /**
207
+ * is_valid function.
208
+ *
209
+ * @access public
210
+ * @return boolean
211
+ */
212
+ public function is_valid() {
213
+ return $this->is_enabled() && $this->has_api_key() && $this->has_list();
214
+ }
215
+
216
+ /**
217
+ * debug_enabled function.
218
+ *
219
+ * @access public
220
+ * @return boolean
221
+ */
222
+ public function debug_enabled() {
223
+ return 'yes' === $this->get_option( 'debug' );
224
+ }
225
+
226
+ /*
227
+ Refreshes the settings form fields
228
+ */
229
+ public function refresh_settings() {
230
+ $this->init_form_fields();
231
+ }
232
+
233
+ /**
234
+ * Check if the user has enabled the plugin functionality, but hasn't provided an api key
235
+ **/
236
+ function checks() {
237
+ // Check required fields
238
+ if ( $this->is_enabled() && ! $this->has_api_key() ) {
239
+ // Show notice
240
+ echo $this->get_message( sprintf( '%s <a href="%s">%s</a>.',
241
+ __( 'WooCommerce MailChimp error: Plugin is enabled but no api key provided. Please enter your api key', 'ss_wc_mailchimp'),
242
+ WOOCOMMERCE_MAILCHIMP_SETTINGS_URL,
243
+ __( 'here', 'ss_wc_mailchimp' )
244
+ )
245
+ );
246
+ }
247
+ }
248
+
249
+ /**
250
+ * order_status_changed function.
251
+ *
252
+ * @access public
253
+ * @return void
254
+ */
255
+ public function order_status_changed( $id, $status = 'new', $new_status = 'pending' ) {
256
+ if ( $this->is_valid() && $new_status === $this->occurs() ) {
257
+ // Get WC order
258
+ $order = $this->wc_get_order( $id );
259
+
260
+ // get the ss_wc_mailchimp_opt_in value from the post meta. "order_custom_fields" was removed with WooCommerce 2.1
261
+ $subscribe_customer = get_post_meta( $id, 'ss_wc_mailchimp_opt_in', true );
262
+
263
+ // If the 'ss_wc_mailchimp_opt_in' meta value isn't set
264
+ // (because 'display_opt_in' wasn't enabled at the time the order was placed)
265
+ // or the 'ss_wc_mailchimp_opt_in' is yes, subscriber the customer
266
+ if ( ! $subscribe_customer || empty( $subscribe_customer ) || 'yes' === $subscribe_customer ) {
267
+ // log
268
+ $this->log( sprintf( __( __METHOD__ . '(): Subscribing customer (%s) to list %s', 'ss_wc_mailchimp' ), $order->billing_email, $this->list() ) );
269
+
270
+ // subscribe
271
+ $this->subscribe( $order->id, $order->billing_first_name, $order->billing_last_name, $order->billing_email, $this->list() );
272
+ }
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Initialize Settings Form Fields
278
+ *
279
+ * @access public
280
+ * @return void
281
+ */
282
+ function init_form_fields() {
283
+ // $this->load_settings();
284
+
285
+ $form_fields = array();
286
+
287
+ $form_fields['api_key'] = array(
288
+ 'title' => __( 'API Key', 'ss_wc_mailchimp' ),
289
+ 'type' => 'text',
290
+ 'description' => sprintf( '<a href="https://admin.mailchimp.com/account/api/" target="_blank">%s</a> %s',
291
+ __( 'Login to MailChimp', 'ss_wc_mailchimp'),
292
+ __( 'to look up your api key.', 'ss_wc_mailchimp' )
293
+ ),
294
+ 'default' => ''
295
+ );
296
+ if ( !$this->has_api_key() ) {
297
+ $form_fields['api_key']['description'] = sprintf( '%s <strong>%s</strong> %s.<br/>',
298
+ __( 'Paste your API key above and click', 'ss_wc_mailchimp' ),
299
+ __( 'Save changes', 'ss_wc_mailchimp' ),
300
+ __( 'below', 'ss_wc_mailchimp' )
301
+ ) . $form_fields['api_key']['description'];
302
+ }
303
+
304
+ $mailchimp_lists = $this->get_lists();
305
+
306
+ if ( is_admin() && ! is_ajax() ) {
307
+
308
+ if ( $this->has_api_key() && $mailchimp_lists !== false ) {
309
+
310
+ if ( $this->has_api_key() && $this->has_list() ) {
311
+ $interest_groups = $this->get_interest_groups();
312
+ } else {
313
+ $interest_groups = array();
314
+ }
315
+
316
+ $form_fields['enabled'] = array(
317
+ 'title' => __( 'Enable/Disable', 'ss_wc_mailchimp' ),
318
+ 'label' => __( 'Enable MailChimp Integration', 'ss_wc_mailchimp' ),
319
+ 'type' => 'checkbox',
320
+ 'description' => __( 'Enable/disable the plugin functionality.', 'ss_wc_mailchimp' ),
321
+ 'default' => 'yes',
322
+ );
323
+
324
+ $form_fields['list'] = array(
325
+ 'title' => __( 'Main List', 'ss_wc_mailchimp' ),
326
+ 'type' => 'select',
327
+ 'description' => __( 'All customers will be added to this list.', 'ss_wc_mailchimp' ),
328
+ 'default' => '',
329
+ 'options' => $mailchimp_lists,
330
+ 'class' => 'wc-enhanced-select',
331
+ 'css' => 'min-width: 350px;',
332
+ 'custom_attributes' => array(
333
+ 'onchange' => 'form.submit()',
334
+ ),
335
+ );
336
+ if ( array_key_exists( 'no_lists', $mailchimp_lists ) ) {
337
+ $form_fields['list']['description'] = sprintf( __( 'There are no lists in your MailChimp account. <a href="%s" target="_blank">Click here</a> to create one.', 'ss_wc_mailchimp' ), 'https://admin.mailchimp.com/lists/new-list/' );
338
+ }
339
+
340
+ $form_fields['interest_groups'] = array(
341
+ 'title' => __( 'Interest Groups', 'ss_wc_mailchimp' ),
342
+ 'type' => 'multiselect',
343
+ 'description' => __( 'Optional: Interest groups to assign to subscribers.', 'ss_wc_mailchimp' ),
344
+ 'default' => '',
345
+ 'options' => $interest_groups,
346
+ 'class' => 'wc-enhanced-select',
347
+ 'custom_attributes' => array(
348
+ 'placeholder' => __( 'Select interest groups...', 'ss_wc_mailchimp' ),
349
+ ),
350
+ 'css' => 'min-width: 350px;',
351
+ );
352
+
353
+ if ( is_array( $interest_groups ) && count( $interest_groups ) == 0 ) {
354
+ // $form_fields['interest_groups']['description'] = __( 'Optional: Interest groups to assign to subscribers.', 'ss_wc_mailchimp' );
355
+ $form_fields['interest_groups']['custom_attributes']['placeholder'] = __( 'This list has no interest groups.', 'ss_wc_mailchimp' );
356
+ $form_fields['interest_groups']['custom_attributes']['disabled'] = 'disabled';
357
+ } elseif ( !$this->has_list() ) {
358
+ $form_fields['interest_groups']['custom_attributes']['placeholder'] = __( 'Select a list to see interests', 'ss_wc_mailchimp' );
359
+ $form_fields['interest_groups']['custom_attributes']['disabled'] = 'disabled';
360
+ }
361
+
362
+ $form_fields['occurs'] = array(
363
+ 'title' => __( 'Subscribe Event', 'ss_wc_mailchimp' ),
364
+ 'type' => 'select',
365
+ 'description' => __( 'When should customers be subscribed to lists?', 'ss_wc_mailchimp' ),
366
+ 'default' => 'pending',
367
+ 'options' => array(
368
+ 'pending' => __( 'Order Created', 'ss_wc_mailchimp' ),
369
+ 'processing' => __( 'Order Processing', 'ss_wc_mailchimp' ),
370
+ 'completed' => __( 'Order Completed', 'ss_wc_mailchimp' ),
371
+ ),
372
+ );
373
+
374
+ $form_fields['double_optin'] = array(
375
+ 'title' => __( 'Double Opt-In', 'ss_wc_mailchimp' ),
376
+ 'label' => __( 'Enable Double Opt-In', 'ss_wc_mailchimp' ),
377
+ 'type' => 'checkbox',
378
+ 'description' => __( 'If enabled, customers will receive an email prompting them to confirm their subscription to the list above.', 'ss_wc_mailchimp' ),
379
+ 'default' => 'no'
380
+ );
381
+
382
+ $form_fields['display_opt_in'] = array(
383
+ 'title' => __( 'Display Opt-In Field', 'ss_wc_mailchimp' ),
384
+ 'label' => __( 'Display an Opt-In Field on Checkout', 'ss_wc_mailchimp' ),
385
+ 'type' => 'checkbox',
386
+ 'description' => __( 'If enabled, customers will be presented with a "Opt-in" checkbox during checkout and will only be added to the list above if they opt-in.', 'ss_wc_mailchimp' ),
387
+ 'default' => 'no',
388
+ );
389
+
390
+ $form_fields['opt_in_label'] = array(
391
+ 'title' => __( 'Opt-In Field Label', 'ss_wc_mailchimp' ),
392
+ 'type' => 'text',
393
+ 'description' => __( 'Optional: customize the label displayed next to the opt-in checkbox.', 'ss_wc_mailchimp' ),
394
+ 'default' => __( 'Subscribe to our newsletter', 'ss_wc_mailchimp' ),
395
+ );
396
+
397
+ $form_fields['opt_in_checkbox_default_status'] = array(
398
+ 'title' => __( 'Opt-In Checkbox Default Status', 'ss_wc_mailchimp' ),
399
+ 'type' => 'select',
400
+ 'description' => __( 'The default state of the opt-in checkbox.', 'ss_wc_mailchimp' ),
401
+ 'default' => 'checked',
402
+ 'options' => array(
403
+ 'checked' => __( 'Checked', 'ss_wc_mailchimp' ),
404
+ 'unchecked' => __( 'Unchecked', 'ss_wc_mailchimp' )
405
+ )
406
+ );
407
+
408
+ $form_fields['opt_in_checkbox_display_location'] = array(
409
+ 'title' => __( 'Opt-In Checkbox Display Location', 'ss_wc_mailchimp' ),
410
+ 'type' => 'select',
411
+ 'description' => __( 'Where to display the opt-in checkbox on the checkout page.', 'ss_wc_mailchimp' ),
412
+ 'default' => 'woocommerce_review_order_before_submit',
413
+ 'options' => array(
414
+ 'woocommerce_checkout_before_customer_details' => __( 'Above customer details', 'ss_wc_mailchimp' ),
415
+ 'woocommerce_checkout_after_customer_details' => __( 'Below customer details', 'ss_wc_mailchimp' ),
416
+ 'woocommerce_review_order_before_submit' => __( 'Order review above submit', 'ss_wc_mailchimp' ),
417
+ 'woocommerce_review_order_after_submit' => __( 'Order review below submit', 'ss_wc_mailchimp' ),
418
+ 'woocommerce_review_order_before_order_total' => __( 'Order review above total', 'ss_wc_mailchimp' ),
419
+ 'woocommerce_checkout_billing' => __( 'Above billing details', 'ss_wc_mailchimp' ),
420
+ 'woocommerce_checkout_shipping' => __( 'Above shipping details', 'ss_wc_mailchimp' ),
421
+ 'woocommerce_after_checkout_billing_form' => __( 'Below Checkout billing form', 'ss_wc_mailchimp' ),
422
+ )
423
+ );
424
+
425
+ $label = __( 'Enable Logging', 'ss_wc_mailchimp' );
426
+
427
+ if ( defined( 'WC_LOG_DIR' ) ) {
428
+ $debug_log_url = add_query_arg( 'tab', 'logs', add_query_arg( 'page', 'wc-status', admin_url( 'admin.php' ) ) );
429
+ $debug_log_key = 'woocommerce-mailchimp-' . sanitize_file_name( wp_hash( 'woocommerce-mailchimp' ) ) . '-log';
430
+ $debug_log_url = add_query_arg( 'log_file', $debug_log_key, $debug_log_url );
431
+
432
+ $label .= ' | ' . sprintf( __( '%1$sView Log%2$s', 'ss_wc_mailchimp' ), '<a href="' . esc_url( $debug_log_url ) . '">', '</a>' );
433
+ }
434
+
435
+ $form_fields[ 'debug' ] = array(
436
+ 'title' => __( 'Debug Log', 'ss_wc_mailchimp' ),
437
+ 'label' => $label,
438
+ 'description' => __( 'Enable logging MailChimp API calls. Only enable for troubleshooting purposes.', 'ss_wc_mailchimp' ),
439
+ 'type' => 'checkbox',
440
+ 'default' => 'no'
441
+ );
442
+ }
443
+
444
+ $this->form_fields = $form_fields;
445
+
446
+ $this->wc_enqueue_js("
447
+ jQuery('#woocommerce_mailchimp_display_opt_in').change(function() {
448
+ if ( jQuery(this).prop('checked') === true ) {
449
+ jQuery('#mainform [id^=woocommerce_mailchimp_opt_in]').closest('tr').show('fast');
450
+ } else {
451
+ jQuery('#mainform [id^=woocommerce_mailchimp_opt_in]').closest('tr').hide('fast');
452
+ }
453
+
454
+ });
455
+
456
+ jQuery('#woocommerce_mailchimp_display_opt_in').change();
457
+ ");
458
+ }
459
+
460
+ } // End init_form_fields()
461
+
462
+ /**
463
+ * WooCommerce 2.1 support for wc_enqueue_js
464
+ *
465
+ * @since 1.2.1
466
+ *
467
+ * @access private
468
+ * @param string $code
469
+ * @return void
470
+ */
471
+ private function wc_enqueue_js( $code ) {
472
+ if ( function_exists( 'wc_enqueue_js' ) ) {
473
+ wc_enqueue_js( $code );
474
+ } else {
475
+ global $woocommerce;
476
+ $woocommerce->add_inline_js( $code );
477
+ }
478
+ }
479
+
480
+ /**
481
+ * WooCommerce 2.2 support for wc_get_order
482
+ *
483
+ * @since 1.2.1
484
+ *
485
+ * @access private
486
+ * @param int $order_id
487
+ * @return void
488
+ */
489
+ private function wc_get_order( $order_id ) {
490
+ if ( function_exists( 'wc_get_order' ) ) {
491
+ return wc_get_order( $order_id );
492
+ } else {
493
+ return new WC_Order( $order_id );
494
+ }
495
+ }
496
+
497
+ /**
498
+ * Get message
499
+ * @return string Error
500
+ */
501
+ private function get_message( $message, $type = 'error' ) {
502
+ ob_start();
503
+
504
+ ?>
505
+ <div class="<?php echo $type ?>">
506
+ <p><?php echo $message ?></p>
507
+ </div>
508
+ <?php
509
+ return ob_get_clean();
510
+ }
511
+
512
+ /**
513
+ * API Instance Singleton
514
+ * @return Object
515
+ */
516
+ public function api() {
517
+ if ( is_null( self::$api_instance ) ) {
518
+ if ( ! $this->has_api_key() ) {
519
+ return false;
520
+ }
521
+ require_once( 'class-ss-wc-mailchimp-api.php' );
522
+ self::$api_instance = new SS_WC_MailChimp_API( $this->api_key(), $this->debug_enabled() );
523
+ }
524
+ return self::$api_instance;
525
+ }
526
+
527
+ /**
528
+ * get_lists function.
529
+ *
530
+ * @access public
531
+ * @return void
532
+ */
533
+ public function get_lists() {
534
+
535
+ // $mailchimp_lists = get_transient( 'ss_wc_mailchimp_lists' );
536
+
537
+ // if ( ! $mailchimp_lists ) {
538
+ if ( $this->api() ) {
539
+ $mailchimp_lists = $this->api()->get_lists();
540
+ } else {
541
+ return false;
542
+ }
543
+
544
+ if ( $mailchimp_lists === false ) {
545
+
546
+ add_action( 'admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
547
+ add_action( 'network_admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
548
+
549
+ return false;
550
+
551
+ }
552
+
553
+ if ( count( $mailchimp_lists ) === 0 ) {
554
+ $default = array(
555
+ 'no_lists' => __( 'Oops! No lists in your MailChimp account...', 'ss_wc_mailchimp' ),
556
+ );
557
+ add_action( 'admin_notices', array( $this, 'mailchimp_no_lists_found' ) );
558
+ } else {
559
+ $default = array(
560
+ '' => __( 'Select a list...', 'ss_wc_mailchimp' ),
561
+ );
562
+ set_transient( 'ss_wc_mailchimp_lists', $mailchimp_lists, 60 * 60 * 1 );
563
+ }
564
+ $mailchimp_lists = array_merge( $default, $mailchimp_lists );
565
+
566
+ //}
567
+
568
+ return $mailchimp_lists;
569
+
570
+ }
571
+
572
+ /**
573
+ * get_interest_groups function.
574
+ *
575
+ * @access public
576
+ * @return void
577
+ */
578
+ public function get_interest_groups() {
579
+
580
+ if ( $this->api() && $this->has_list() ) {
581
+ $interest_groups = $this->api()->get_interest_categories_with_interests( $this->list() );
582
+ } else {
583
+ return false;
584
+ }
585
+
586
+ if ( $interest_groups === false ) {
587
+
588
+ add_action( 'admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
589
+ add_action( 'network_admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
590
+
591
+ return false;
592
+
593
+ }
594
+
595
+ return $interest_groups;
596
+
597
+ }
598
+
599
+ /**
600
+ * Inform the user they don't have any MailChimp lists
601
+ */
602
+ public function mailchimp_no_lists_found() {
603
+ echo $this->get_message( sprintf( __( 'Oops! There are no lists in your MailChimp account. <a href="%s" target="_blank">Click here</a> to create one.', 'ss_wc_mailchimp' ), 'https://admin.mailchimp.com/lists/new-list/' ) );
604
+ }
605
+
606
+ /**
607
+ * Display message to user if there is an issue with the MailChimp API call
608
+ *
609
+ * @since 1.0
610
+ * @param void
611
+ * @return html the message for the user
612
+ */
613
+ public function mailchimp_api_error_msg() {
614
+ echo $this->get_message(
615
+ sprintf( __( 'Unable to load lists from MailChimp: (%s) %s. ', 'ss_wc_mailchimp' ), $this->api()->get_error_code(), $this->api()->get_error_message() ) .
616
+ sprintf( __( 'Please check your %s <a href="%s">settings</a>.', 'ss_wc_mailchimp' ), __( 'Settings', 'ss_wc_mailchimp' ), WOOCOMMERCE_MAILCHIMP_SETTINGS_URL )
617
+ );
618
+ } //end function mailchimp_api_error_msg
619
+
620
+ /**
621
+ * subscribe function.
622
+ *
623
+ * @access public
624
+ * @param int $order_id
625
+ * @param mixed $first_name
626
+ * @param mixed $last_name
627
+ * @param mixed $email
628
+ * @param string $listid (default: 'false')
629
+ * @return void
630
+ */
631
+ public function subscribe( $order_id, $first_name, $last_name, $email, $list_id = 'false' ) {
632
+ if ( ! $email ) {
633
+ return; // Email is required
634
+ }
635
+
636
+ if ( 'false' == $list_id ) {
637
+ $list_id = $this->list();
638
+ }
639
+
640
+ $merge_tags = array(
641
+ 'FNAME' => $first_name,
642
+ 'LNAME' => $last_name
643
+ );
644
+
645
+ if ( ! empty( $this->interest_groups() ) ) {
646
+ $interest_groups = array_fill_keys( $this->interest_groups(), true );
647
+
648
+ // Allow hooking into variables
649
+ $interest_groups = apply_filters( 'ss_wc_mailchimp_subscribe_interest_groups', $interest_groups, $order_id, $email );
650
+ }
651
+
652
+ // Allow hooking into variables
653
+ $merge_tags = apply_filters( 'ss_wc_mailchimp_subscribe_merge_tags', $merge_tags, $order_id, $email );
654
+
655
+ // Set subscription options
656
+ $subscribe_options = array(
657
+ 'list_id' => $list_id,
658
+ 'email' => $email,
659
+ 'merge_tags' => $merge_tags,
660
+ 'interest_groups' => $interest_groups,
661
+ 'email_type' => 'html',
662
+ 'double_optin' => $this->double_optin(),
663
+ );
664
+
665
+ // Allow hooking into subscription options
666
+ $options = apply_filters( 'ss_wc_mailchimp_subscribe_options', $subscribe_options, $order_id );
667
+
668
+ // Extract options into variables
669
+ extract( $options );
670
+
671
+ // Log
672
+ $this->log( sprintf( __( __METHOD__ . '(): Subscribing customer to MailChimp: %s', 'ss_wc_mailchimp' ), print_r( $options, true ) ) );
673
+
674
+ // Call API
675
+ $api_response = $this->api()->subscribe( $list_id, $email, $email_type, $merge_fields, $interests, $double_optin );
676
+
677
+ // Log api response
678
+ $this->log( sprintf( __( __METHOD__ . '(): MailChimp API response: %s', 'ss_wc_mailchimp' ), $api_response ) );
679
+
680
+ if ( $api_response === false ) {
681
+ // Format error message
682
+ $error_response = sprintf( __( __METHOD__ . '(): WooCommerce MailChimp subscription failed: %s (%s)', 'ss_wc_mailchimp' ), $this->api()->get_error_message(), $this->api()->get_error_code() );
683
+
684
+ // Log
685
+ $this->log( $error_response );
686
+
687
+ // New hook for failing operations
688
+ do_action( 'ss_wc_mailchimp_subscription_failed', $email, array( 'list_id' => $list_id, 'order_id' => $order_id ) );
689
+
690
+ // Email admin
691
+ wp_mail( get_option( 'admin_email' ), __( 'WooCommerce MailChimp subscription failed', 'ss_wc_mailchimp' ), $error_response );
692
+ } else {
693
+ // Hook on success
694
+ do_action( 'ss_wc_mailchimp_subscription_success', $email, array( 'list_id' => $list_id, 'order_id' => $order_id ) );
695
+ }
696
+ }
697
+
698
+ /**
699
+ * Admin Panel Options
700
+ */
701
+ function admin_options() {
702
+ ?>
703
+ <h3><?php _e( 'MailChimp', 'ss_wc_mailchimp' ); ?></h3>
704
+ <p><?php _e( 'Enter your MailChimp settings below to control how WooCommerce integrates with your MailChimp lists.', 'ss_wc_mailchimp' ); ?></p>
705
+ <table class="form-table">
706
+ <?php $this->generate_settings_html(); ?>
707
+ </table><!--/.form-table-->
708
+ <?php
709
+ }
710
+
711
+ /**
712
+ * Add the opt-in checkbox to the checkout fields (to be displayed on checkout).
713
+ *
714
+ * @since 1.1
715
+ */
716
+ function maybe_add_checkout_fields() {
717
+ if ( $this->is_valid() ) {
718
+ if ( $this->display_opt_in() ) {
719
+ do_action( 'ss_wc_mailchimp_before_opt_in_checkbox' );
720
+ echo apply_filters('ss_wc_mailchimp_opt_in_checkbox', '<p class="form-row woocommerce-mailchimp-opt-in"><label for="ss_wc_mailchimp_opt_in"><input type="checkbox" name="ss_wc_mailchimp_opt_in" id="ss_wc_mailchimp_opt_in" value="yes"' . ($this->opt_in_checkbox_default_status() == 'checked' ? ' checked="checked"' : '') . '/> ' . esc_html( $this->opt_in_label() ) . '</label></p>' . "\n", $this->opt_in_checkbox_default_status(), $this->opt_in_label() );
721
+ do_action( 'ss_wc_mailchimp_after_opt_in_checkbox' );
722
+ }
723
+ }
724
+ }
725
+
726
+ /**
727
+ * Opt-in checkbox default support for WooCommerce 2.1
728
+ *
729
+ * @since 1.2.1
730
+ */
731
+ function checkbox_default_status( $input ) {
732
+ return $this->opt_in_checkbox_default_status === 'checked' ? 1 : 0;
733
+ }
734
+
735
+ /**
736
+ * When the checkout form is submitted, save opt-in value.
737
+ *
738
+ * @version 1.1
739
+ */
740
+ function maybe_save_checkout_fields( $order_id ) {
741
+ if ( $this->display_opt_in() ) {
742
+ $opt_in = isset( $_POST['ss_wc_mailchimp_opt_in'] ) ? 'yes' : 'no';
743
+
744
+ update_post_meta( $order_id, 'ss_wc_mailchimp_opt_in', $opt_in );
745
+ }
746
+ }
747
+
748
+ /**
749
+ * Helper log function for debugging
750
+ *
751
+ * @since 1.2.2
752
+ */
753
+ private function log( $message ) {
754
+ if ( $this->debug_enabled() ) {
755
+ $logger = new WC_Logger();
756
+
757
+ if ( is_array( $message ) || is_object( $message ) ) {
758
+ $logger->add( 'woocommerce-mailchimp', print_r( $message, true ) );
759
+ }
760
+ else {
761
+ $logger->add( 'woocommerce-mailchimp', $message );
762
+ }
763
+ }
764
+ }
765
+
766
+ }
includes/class-ss-wc-mailchimp-api.php ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
+
5
+ /**
6
+ * Minimal MailChimp API v3.0 wrapper
7
+ *
8
+ * @class SS_WC_MailChimp_API
9
+ * @version 2.0
10
+ * @package WooCommerce MailChimp
11
+ * @author Saint Systems
12
+ */
13
+ class SS_WC_MailChimp_API {
14
+
15
+ /**
16
+ * @var string
17
+ */
18
+ public $api_key;
19
+
20
+ /**
21
+ * @var string
22
+ */
23
+ public $datacenter = 'us1';
24
+
25
+ /**
26
+ * @var string
27
+ */
28
+ private $api_root = 'https://<dc>.api.mailchimp.com/3.0/';
29
+
30
+ /**
31
+ * @var boolean
32
+ */
33
+ private $debug = false;
34
+
35
+ /**
36
+ * @var array
37
+ */
38
+ private $last_response;
39
+
40
+ /**
41
+ * @var WP_Error
42
+ */
43
+ private $last_error;
44
+
45
+ /**
46
+ * Create a new instance
47
+ * @param string $api_key MailChimp API key
48
+ * @param boolean $debug Whether or not to log API calls
49
+ */
50
+ function __construct( $api_key, $debug = false ) {
51
+
52
+ $this->debug = $debug;
53
+
54
+ if ( $this->debug ) {
55
+ $this->log = new WC_Logger();
56
+ }
57
+
58
+ $this->api_key = $api_key;
59
+ $api_key_parts = explode( '-', $this->api_key );
60
+ $this->datacenter = empty( $api_key_parts[1] ) ? 'us1' : $api_key_parts[1];
61
+ $this->api_root = str_replace( '<dc>', $this->datacenter, $this->api_root );
62
+
63
+ } //end function __construct
64
+
65
+ /**
66
+ * @param string $resource
67
+ * @param array $args
68
+ *
69
+ * @return mixed
70
+ */
71
+ public function get( $resource, $args = array() ) {
72
+
73
+ if ( ! array_key_exists( 'count', $args ) ) {
74
+ $args['count'] = 10;
75
+ }
76
+
77
+ return $this->api_request( 'GET', $resource, $args );
78
+
79
+ } //end function post
80
+
81
+ /**
82
+ * @param string $resource
83
+ * @param array $args
84
+ *
85
+ * @return mixed
86
+ */
87
+ public function post( $resource, $args = array() ) {
88
+
89
+ return $this->api_request( 'POST', $resource, $args );
90
+
91
+ } //end function post
92
+
93
+ /**
94
+ * @param string $resource
95
+ * @param array $args
96
+ *
97
+ * @return mixed
98
+ */
99
+ public function put( $resource, $args = array() ) {
100
+
101
+ return $this->api_request( 'PUT', $resource, $args );
102
+
103
+ } //end function put
104
+
105
+ /**
106
+ * @param string $resource
107
+ * @param array $args
108
+ *
109
+ * @return mixed
110
+ */
111
+ public function patch( $resource, $args = array() ) {
112
+
113
+ return $this->api_request( 'PATCH', $resource, $args );
114
+
115
+ } //end function patch
116
+
117
+ /**
118
+ * @param string $resource
119
+ * @param array $args
120
+ *
121
+ * @return mixed
122
+ */
123
+ public function delete( $resource, $args = array() ) {
124
+
125
+ return $this->api_request( 'DELETE', $resource, $args );
126
+
127
+ } //end function delete
128
+
129
+ /**
130
+ * Performs the underlying HTTP request.
131
+ * @param string $method HTTP method (GET|POST|PUT|PATCH|DELETE)
132
+ * @param string $resource MailChimp API resource to be called
133
+ * @param array $args array of parameters to be passed
134
+ * @return array array of decoded result
135
+ */
136
+ private function api_request( $method, $resource, $args = array() ) {
137
+
138
+ $this->reset();
139
+
140
+ $url = $this->api_root . $resource;
141
+
142
+ global $wp_version;
143
+
144
+ $request_args = array(
145
+ 'method' => $method,
146
+ 'sslverify' => false,
147
+ 'timeout' => 60,
148
+ 'redirection' => 5,
149
+ 'httpversion' => '1.1',
150
+ 'headers' => array(
151
+ 'Content-Type' => 'application/json',
152
+ 'Accept' => 'application/json',
153
+ 'Authorization' => 'apikey ' . $this->api_key,
154
+ 'User-Agent' => 'woocommerce-mailchimp/' . SS_WC_MAILCHIMP_PLUGIN_VERSION . '; WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ),
155
+ ),
156
+ );
157
+
158
+ // attach arguments (in body or URL)
159
+ if ( $method === 'GET' ) {
160
+ $url = add_query_arg( $args, $url );
161
+ } else {
162
+ $request_args['body'] = json_encode( $args );
163
+ }
164
+
165
+ $raw_response = wp_remote_request( $url, $request_args );
166
+
167
+ $this->last_response = $raw_response;
168
+
169
+ $this->maybe_log( $url, $method, $args, $raw_response );
170
+
171
+ if ( is_wp_error( $raw_response ) ) {
172
+
173
+ $this->last_error = new WP_Error( 'ss-wc-mc-api-request-error', $raw_response->get_error_message(), $this->format_error( $resource, $method, $raw_response ) );
174
+
175
+ return false;
176
+
177
+ } elseif ( is_array( $raw_response )
178
+ && $raw_response['response']['code']
179
+ && floor( $raw_response['response']['code'] ) / 100 >= 4 ) {
180
+
181
+ $json = wp_remote_retrieve_body( $raw_response );
182
+
183
+ $error = json_decode( $json, true );
184
+
185
+ $this->last_error = new WP_Error( 'ss-wc-mc-api-request-error', $error['detail'], $this->format_error( $resource, $method, $raw_response ) );
186
+
187
+ return false;
188
+
189
+ } else {
190
+
191
+ $json = wp_remote_retrieve_body( $raw_response );
192
+
193
+ $result = json_decode( $json, true );
194
+
195
+ return $result;
196
+
197
+ }
198
+
199
+ } //end function api_request
200
+
201
+ /**
202
+ * Empties all data from previous response
203
+ */
204
+ private function reset() {
205
+ $this->last_response = null;
206
+ $this->last_error = null;
207
+ }
208
+
209
+ /**
210
+ * Conditionally log MailChimp API Call
211
+ * @param string $resource MailChimp API Resource
212
+ * @param string $method HTTP Method
213
+ * @param array $args HTTP Request Body
214
+ * @param array $response WP HTTP Response
215
+ * @return void
216
+ */
217
+ private function maybe_log( $resource, $method, $args, $response ) {
218
+
219
+ if ( $this->debug ) {
220
+ $this->log->add( 'woocommerce-mailchimp', "MailChimp API Call RESOURCE: $resource \n METHOD: $method \n BODY: " . print_r( $args, true ) . " \n RESPONSE: " . print_r( $response, true ) );
221
+ }
222
+
223
+ }
224
+
225
+ /**
226
+ * Formats api_request info for inclusion in WP_Error $data
227
+ * @param [type] $resource [description]
228
+ * @param [type] $method [description]
229
+ * @param [type] $response [description]
230
+ * @return [type] [description]
231
+ */
232
+ private function format_error( $resource, $method, $response ) {
233
+ return array(
234
+ 'resource' => $resource,
235
+ 'method' => $method,
236
+ 'response' => json_encode($response),
237
+ );
238
+ }
239
+
240
+ /**
241
+ * has_api_key function.
242
+ *
243
+ * @access public
244
+ * @return void
245
+ */
246
+ public function has_api_key() {
247
+
248
+ return !empty( $this->api_key );
249
+
250
+ } //end function has_api_key
251
+
252
+ /**
253
+ * @return array|WP_Error
254
+ */
255
+ public function get_last_response() {
256
+ return $this->last_response;
257
+ }
258
+
259
+ /**
260
+ * Returns error code from error property
261
+ * @return string error code
262
+ */
263
+ public function get_error_code() {
264
+
265
+ return $this->last_error->get_error_code();
266
+
267
+ } //end get_error_code
268
+
269
+ /**
270
+ * Returns error message from error property
271
+ * @return string error message
272
+ */
273
+ public function get_error_message() {
274
+
275
+ return $this->last_error->get_error_message();
276
+
277
+ } //end get_error_message
278
+
279
+ } //end class SS_WC_MailChimp_API
includes/class-ss-wc-mailchimp-container.php ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Class SS_WC_MailChimp_Container
5
+ *
6
+ * @access private
7
+ * @ignore
8
+ */
9
+ class SS_WC_MailChimp_Container implements ArrayAccess {
10
+
11
+ /**
12
+ * @var array
13
+ */
14
+ protected $services = array();
15
+
16
+ /**
17
+ * @var array
18
+ */
19
+ protected $resolved_services = array();
20
+
21
+ /**
22
+ * @param $name
23
+ * @return boolean
24
+ */
25
+ public function has( $name ) {
26
+ return isset( $this->services[ $name ] );
27
+ }
28
+
29
+ /**
30
+ * @param $name
31
+ *
32
+ * @return mixed
33
+ * @throws Exception
34
+ */
35
+ public function get( $name ) {
36
+
37
+ if( ! $this->has( $name ) ) {
38
+ throw new Exception( sprintf( 'No service named %s was registered.', $name ) );
39
+ }
40
+
41
+ $service = $this->services[ $name ];
42
+
43
+ // is this a resolvable service?
44
+ if( is_callable( $service ) ) {
45
+
46
+ // resolve service if it's not resolved yet
47
+ if( ! isset( $this->resolved_services[ $name ] ) ) {
48
+ $this->resolved_services[ $name ] = call_user_func( $service );
49
+ }
50
+
51
+ return $this->resolved_services[ $name ];
52
+ }
53
+
54
+ return $this->services[ $name ];
55
+ }
56
+
57
+ /**
58
+ * (PHP 5 &gt;= 5.0.0)<br/>
59
+ * Whether a offset exists
60
+ * @link http://php.net/manual/en/arrayaccess.offsetexists.php
61
+ *
62
+ * @param mixed $offset <p>
63
+ * An offset to check for.
64
+ * </p>
65
+ *
66
+ * @return boolean true on success or false on failure.
67
+ * </p>
68
+ * <p>
69
+ * The return value will be casted to boolean if non-boolean was returned.
70
+ */
71
+ public function offsetExists( $offset ) {
72
+ return $this->has( $offset );
73
+ }
74
+
75
+ /**
76
+ * (PHP 5 &gt;= 5.0.0)<br/>
77
+ * Offset to retrieve
78
+ * @link http://php.net/manual/en/arrayaccess.offsetget.php
79
+ *
80
+ * @param mixed $offset <p>
81
+ * The offset to retrieve.
82
+ * </p>
83
+ *
84
+ * @return mixed Can return all value types.
85
+ */
86
+ public function offsetGet( $offset ) {
87
+ return $this->get( $offset );
88
+ }
89
+
90
+ /**
91
+ * (PHP 5 &gt;= 5.0.0)<br/>
92
+ * Offset to set
93
+ * @link http://php.net/manual/en/arrayaccess.offsetset.php
94
+ *
95
+ * @param mixed $offset <p>
96
+ * The offset to assign the value to.
97
+ * </p>
98
+ * @param mixed $value <p>
99
+ * The value to set.
100
+ * </p>
101
+ *
102
+ * @return void
103
+ */
104
+ public function offsetSet( $offset, $value ) {
105
+ $this->services[ $offset ] = $value;
106
+ }
107
+
108
+ /**
109
+ * (PHP 5 &gt;= 5.0.0)<br/>
110
+ * Offset to unset
111
+ * @link http://php.net/manual/en/arrayaccess.offsetunset.php
112
+ *
113
+ * @param mixed $offset <p>
114
+ * The offset to unset.
115
+ * </p>
116
+ *
117
+ * @return void
118
+ */
119
+ public function offsetUnset( $offset ) {
120
+ unset( $this->services[ $offset ] );
121
+ }}
includes/class-ss-wc-mailchimp-handler.php ADDED
@@ -0,0 +1,573 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WooCommerce MailChimp Handler
4
+ *
5
+ * @author Saint Systems
6
+ * @package WooCommerce MailChimp
7
+ * @version 2.0
8
+ */
9
+
10
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
11
+
12
+ if ( ! class_exists( 'SS_WC_MailChimp_Handler' ) ) {
13
+
14
+ /**
15
+ * @class SS_WC_MailChimp_Handler
16
+ */
17
+ class SS_WC_MailChimp_Handler {
18
+
19
+ private static $_instance;
20
+
21
+ /**
22
+ * Singleton instance
23
+ *
24
+ * @return SS_WC_Settings_MailChimp SS_WC_Settings_MailChimp object
25
+ */
26
+ public static function get_instance() {
27
+
28
+ if ( empty( self::$_instance ) ) {
29
+ self::$_instance = new self;
30
+ }
31
+
32
+ return self::$_instance;
33
+ }
34
+
35
+ /**
36
+ * Instance of the API class.
37
+ * @var Object
38
+ */
39
+ private static $api_instance = null;
40
+
41
+ /**
42
+ * Constructor
43
+ *
44
+ * @access public
45
+ * @return void
46
+ */
47
+ public function __construct() {
48
+
49
+ $this->id = 'mailchimp';
50
+ $this->namespace = 'ss_wc_' . $this->id;
51
+ $this->label = __( 'MailChimp', $this->namespace );
52
+
53
+ $this->init();
54
+
55
+ $this->register_hooks();
56
+
57
+ } //end function __construct
58
+
59
+ /**
60
+ * api_key function.
61
+ * @return string MailChimp API Key
62
+ */
63
+ public function api_key() {
64
+ return $this->get_option( 'api_key' );
65
+ }
66
+
67
+ /**
68
+ * is_enabled function.
69
+ *
70
+ * @access public
71
+ * @return boolean
72
+ */
73
+ public function is_enabled() {
74
+ return 'yes' === $this->get_option( 'enabled' );
75
+ }
76
+
77
+ /**
78
+ * occurs function
79
+ * @return string
80
+ */
81
+ public function occurs() {
82
+ return $this->get_option( 'occurs' );
83
+ }
84
+
85
+ /**
86
+ * list function.
87
+ *
88
+ * @access public
89
+ * @return string MailChimp list ID
90
+ */
91
+ public function list() {
92
+ return $this->get_option( 'list' );
93
+ }
94
+
95
+ /**
96
+ * double_optin function.
97
+ *
98
+ * @access public
99
+ * @return boolean
100
+ */
101
+ public function double_optin() {
102
+ return 'yes' === $this->get_option( 'double_optin' );
103
+ }
104
+
105
+ /**
106
+ * display_opt_in function.
107
+ *
108
+ * @access public
109
+ * @return boolean
110
+ */
111
+ public function display_opt_in() {
112
+ return 'yes' === $this->get_option( 'display_opt_in' );
113
+ }
114
+
115
+ /**
116
+ * opt_in_label function.
117
+ *
118
+ * @access public
119
+ * @return string
120
+ */
121
+ public function opt_in_label() {
122
+ return $this->get_option( 'opt_in_label' );
123
+ }
124
+
125
+ /**
126
+ * opt_in_checkbox_default_status function.
127
+ *
128
+ * @access public
129
+ * @return string
130
+ */
131
+ public function opt_in_checkbox_default_status() {
132
+ return $this->get_option( 'opt_in_checkbox_default_status' );
133
+ }
134
+
135
+ /**
136
+ * opt_in_checkbox_display_location function.
137
+ *
138
+ * @access public
139
+ * @return string
140
+ */
141
+ public function opt_in_checkbox_display_location() {
142
+ return $this->get_option( 'opt_in_checkbox_display_location' );
143
+ }
144
+
145
+ /**
146
+ * interests function.
147
+ *
148
+ * @access public
149
+ * @return array
150
+ */
151
+ public function interest_groups() {
152
+ return $this->get_option( 'interest_groups' );
153
+ }
154
+
155
+ /**
156
+ * has_list function.
157
+ *
158
+ * @access public
159
+ * @return boolean
160
+ */
161
+ public function has_list() {
162
+ if ( $this->list() ) {
163
+ return true;
164
+ }
165
+ return false;
166
+ }
167
+
168
+ /**
169
+ * has_api_key function.
170
+ *
171
+ * @access public
172
+ * @return boolean
173
+ */
174
+ public function has_api_key() {
175
+ return !empty( $this->api_key() );
176
+ }
177
+
178
+ /**
179
+ * is_valid function.
180
+ *
181
+ * @access public
182
+ * @return boolean
183
+ */
184
+ public function is_valid() {
185
+ return $this->is_enabled() && $this->has_api_key() && $this->has_list();
186
+ }
187
+
188
+ /**
189
+ * debug_enabled function.
190
+ *
191
+ * @access public
192
+ * @return boolean
193
+ */
194
+ public function debug_enabled() {
195
+ return 'yes' === $this->get_option( 'debug' );
196
+ }
197
+
198
+ /**
199
+ * Check if the user has enabled the plugin functionality, but hasn't provided an api key
200
+ **/
201
+ function checks() {
202
+ // Check required fields
203
+ if ( $this->is_enabled() && ! $this->has_api_key() ) {
204
+ // Show notice
205
+ echo $this->get_message( sprintf( '%s <a href="%s">%s</a>.',
206
+ __( 'WooCommerce MailChimp error: Plugin is enabled but no api key provided. Please enter your api key', $this->namespace ),
207
+ WOOCOMMERCE_MAILCHIMP_SETTINGS_URL,
208
+ __( 'here', $this->namespace )
209
+ )
210
+ );
211
+ }
212
+ }
213
+
214
+ /**
215
+ * order_status_changed function.
216
+ *
217
+ * @access public
218
+ * @return void
219
+ */
220
+ public function order_status_changed( $id, $status = 'new', $new_status = 'pending' ) {
221
+ if ( $this->is_valid() && $new_status === $this->occurs() ) {
222
+ // Get WC order
223
+ $order = $this->wc_get_order( $id );
224
+
225
+ // get the ss_wc_mailchimp_opt_in value from the post meta. "order_custom_fields" was removed with WooCommerce 2.1
226
+ $subscribe_customer = get_post_meta( $id, $this->namespace_prefixed( 'opt_in' ), true );
227
+
228
+ // If the 'ss_wc_mailchimp_opt_in' meta value isn't set
229
+ // (because 'display_opt_in' wasn't enabled at the time the order was placed)
230
+ // or the 'ss_wc_mailchimp_opt_in' is yes, subscriber the customer
231
+ if ( ! $subscribe_customer || empty( $subscribe_customer ) || 'yes' === $subscribe_customer ) {
232
+ // log
233
+ $this->log( sprintf( __( __METHOD__ . '(): Subscribing customer (%s) to list %s', $this->namespace ), $order->billing_email, $this->list() ) );
234
+
235
+ // subscribe
236
+ $this->subscribe( $order->id, $order->billing_first_name, $order->billing_last_name, $order->billing_email, $this->list() );
237
+ }
238
+ }
239
+ }
240
+
241
+ public function init() {
242
+
243
+ $this->api_key = $this->get_option( 'api_key' );
244
+
245
+ $this->enabled = $this->get_option( 'enabled' );
246
+
247
+ }
248
+
249
+ public function get_option( $option_suffix ) {
250
+
251
+ return get_option( $this->namespace_prefixed( $option_suffix ) );
252
+
253
+ }
254
+
255
+ /**
256
+ * Register plugin hooks
257
+ *
258
+ * @access public
259
+ * @return void
260
+ */
261
+ public function register_hooks() {
262
+
263
+ // We would use the 'woocommerce_new_order' action but first name, last name and email address (order meta) is not yet available,
264
+ // so instead we use the 'woocommerce_checkout_update_order_meta' action hook which fires after the checkout process on the "thank you" page
265
+ add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'order_status_changed' ), 1000, 1 );
266
+
267
+ // hook into woocommerce order status changed hook to handle the desired subscription event trigger
268
+ add_action( 'woocommerce_order_status_changed', array( $this, 'order_status_changed' ), 10, 3 );
269
+
270
+ // Maybe add an "opt-in" field to the checkout
271
+ $opt_in_checkbox_display_location = !empty( $this->opt_in_checkbox_display_location() ) ? $this->opt_in_checkbox_display_location() : 'woocommerce_review_order_before_submit';
272
+
273
+ // Old opt-in checkbox display locations
274
+ $old_opt_in_checkbox_display_locations = array(
275
+ 'billing' => 'woocommerce_after_checkout_billing_form',
276
+ 'order' => 'woocommerce_review_order_before_submit',
277
+ );
278
+
279
+ // Map old billing/order checkbox display locations to new format
280
+ if ( array_key_exists( $opt_in_checkbox_display_location, $old_opt_in_checkbox_display_locations ) ) {
281
+ $opt_in_checkbox_display_location = $old_opt_in_checkbox_display_locations[ $opt_in_checkbox_display_location ];
282
+ }
283
+
284
+ add_action( $opt_in_checkbox_display_location, array( $this, 'maybe_add_checkout_fields' ) );
285
+ add_filter( 'default_checkout_ss_wc_mailchimp_opt_in', array( $this, 'checkbox_default_status' ) );
286
+
287
+ // Maybe save the "opt-in" field on the checkout
288
+ add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'maybe_save_checkout_fields' ) );
289
+
290
+ add_action( 'wp_ajax_ss_wc_mailchimp_get_lists', array( $this, 'ajax_get_lists' ) );
291
+ add_action( 'wp_ajax_ss_wc_mailchimp_get_interest_groups', array( $this, 'ajax_get_interest_groups' ) );
292
+
293
+ } //end function ensure_tab
294
+
295
+ /**
296
+ * Return all lists from MailChimp to be used in select fields
297
+ *
298
+ * @access public
299
+ * @return array
300
+ */
301
+ public function ajax_get_lists() {
302
+
303
+ try {
304
+
305
+ if ( !$_POST['data']['api_key'] || empty( $_POST['data']['api_key'] ) ) {
306
+
307
+ return $this->toJSON( array( '' => __( 'Enter your api key above to see your lists', $this->namespace ) ) );
308
+
309
+ }
310
+
311
+ $api_key = $_POST['data']['api_key'];
312
+
313
+ $lists = $this->api( $api_key )->get_lists();
314
+
315
+ $results = array_merge( array('' => 'Select a list...'), $lists );
316
+
317
+ }
318
+ catch ( Exception $e ) {
319
+
320
+ return $this->toJSON( array( 'error' => $e->getMessage() ) );
321
+
322
+ }
323
+
324
+ return $this->toJSON( $results );
325
+
326
+ } //end function ajax_get_lists
327
+
328
+ /**
329
+ * Return interest categories for the passed MailChimp List to be used in select fields
330
+ *
331
+ * @access public
332
+ * @return array
333
+ */
334
+ public function ajax_get_interest_groups() {
335
+
336
+ try {
337
+
338
+ if ( !$_POST['data']['api_key'] || empty( $_POST['data']['api_key'] ) ) {
339
+
340
+ return $this->toJSON( array( '' => __( 'Enter your api key above to see your lists', 'ss_wc_mailchimp' ) ) );
341
+
342
+ }
343
+
344
+ if ( !$_POST['data']['list_id'] || empty( $_POST['data']['list_id'] ) ) {
345
+
346
+ return $this->toJSON( array( '' => __( 'Please select a list from above.', 'ss_wc_mailchimp' ) ) );
347
+
348
+ }
349
+
350
+ $api_key = $_POST['data']['api_key'];
351
+
352
+ $list_id = $_POST['data']['list_id'];
353
+
354
+ $interest_groups = $this->api( $api_key )->get_interest_categories_with_interests( $list_id );
355
+
356
+ $results = $interest_groups;
357
+
358
+ }
359
+ catch ( Exception $e ) {
360
+
361
+ return $this->toJSON( array( 'error' => $e->getMessage() ) );
362
+
363
+ }
364
+
365
+ return $this->toJSON( $results );
366
+
367
+ } //end function ajax_get_interest_groups
368
+
369
+ private function toJSON( $response ) {
370
+
371
+ // Commented out due to json_encode not preserving quotes around MailChimp ids
372
+ // header('Content-Type: application/json');
373
+ echo json_encode( $response );
374
+ exit();
375
+
376
+ } //end function toJSON
377
+
378
+ private function namespace_prefixed( $suffix ) {
379
+ return $this->namespace . '_' . $suffix;
380
+ }
381
+
382
+ /**
383
+ * WooCommerce 2.2 support for wc_get_order
384
+ *
385
+ * @since 1.2.1
386
+ *
387
+ * @access private
388
+ * @param int $order_id
389
+ * @return void
390
+ */
391
+ private function wc_get_order( $order_id ) {
392
+ if ( function_exists( 'wc_get_order' ) ) {
393
+ return wc_get_order( $order_id );
394
+ } else {
395
+ return new WC_Order( $order_id );
396
+ }
397
+ }
398
+
399
+ /**
400
+ * Get message
401
+ * @return string Error
402
+ */
403
+ private function get_message( $message, $type = 'error' ) {
404
+ ob_start();
405
+
406
+ ?>
407
+ <div class="<?php echo $type ?>">
408
+ <p><?php echo $message ?></p>
409
+ </div>
410
+ <?php
411
+ return ob_get_clean();
412
+ }
413
+
414
+ /**
415
+ * API Instance Singleton
416
+ * @return Object
417
+ */
418
+ public function api( $api_key = null ) {
419
+ // if ( is_null( self::$api_instance ) ) {
420
+ // if ( ! $this->has_api_key() && empty( $api_key ) ) {
421
+ // return false;
422
+ // }
423
+ // require_once( 'class-ss-wc-mailchimp-api.php' );
424
+ // self::$api_instance = new SS_WC_MailChimp_API( ( $api_key ? $api_key : $this->api_key() ), $this->debug_enabled() );
425
+ // }
426
+ // return self::$api_instance;
427
+ if ( ! is_null( $api_key ) ) {
428
+ global $ss_wc_mailchimp;
429
+
430
+ // $ss_wc_mailchimp['api'] = new SS_WC_MailChimp( $api_key , $this->debug_enabled() );
431
+ $ss_wc_mailchimp['api'] = ss_wc_mailchimp_get_api( $api_key , $this->debug_enabled() );
432
+
433
+ delete_transient( 'sswcmc_lists' );
434
+ }
435
+ return ss_wc_mailchimp('api');
436
+ }
437
+
438
+ /**
439
+ * subscribe function.
440
+ *
441
+ * @access public
442
+ * @param int $order_id
443
+ * @param mixed $first_name
444
+ * @param mixed $last_name
445
+ * @param mixed $email
446
+ * @param string $listid (default: 'false')
447
+ * @return void
448
+ */
449
+ public function subscribe( $order_id, $first_name, $last_name, $email, $list_id = 'false' ) {
450
+ if ( ! $email ) {
451
+ return; // Email is required
452
+ }
453
+
454
+ if ( 'false' == $list_id ) {
455
+ $list_id = $this->list();
456
+ }
457
+
458
+ $merge_tags = array(
459
+ 'FNAME' => $first_name,
460
+ 'LNAME' => $last_name
461
+ );
462
+
463
+ if ( ! empty( $this->interest_groups() ) ) {
464
+ $interest_groups = array_fill_keys( $this->interest_groups(), true );
465
+
466
+ // Allow hooking into variables
467
+ $interest_groups = apply_filters( $this->namespace_prefixed( 'subscribe_interest_groups' ), $interest_groups, $order_id, $email );
468
+ }
469
+
470
+ // Allow hooking into variables
471
+ $merge_tags = apply_filters( $this->namespace_prefixed( 'subscribe_merge_tags' ), $merge_tags, $order_id, $email );
472
+
473
+ // Set subscription options
474
+ $subscribe_options = array(
475
+ 'list_id' => $list_id,
476
+ 'email' => $email,
477
+ 'merge_tags' => $merge_tags,
478
+ 'interest_groups' => $interest_groups,
479
+ 'email_type' => 'html',
480
+ 'double_optin' => $this->double_optin(),
481
+ );
482
+
483
+ // Allow hooking into subscription options
484
+ $options = apply_filters( $this->namespace_prefixed( 'subscribe_options' ), $subscribe_options, $order_id );
485
+
486
+ // Extract options into variables
487
+ extract( $options );
488
+
489
+ // Log
490
+ $this->log( sprintf( __( __METHOD__ . '(): Subscribing customer to MailChimp: %s', $this->namespace ), print_r( $options, true ) ) );
491
+
492
+ // Call API
493
+ $api_response = $this->api()->subscribe( $list_id, $email, $email_type, $merge_tags, $interest_groups, $double_optin );
494
+
495
+ // Log api response
496
+ $this->log( sprintf( __( __METHOD__ . '(): MailChimp API response: %s', $this->namespace ), print_r( $api_response, true ) ) );
497
+
498
+ if ( $api_response === false ) {
499
+ // Format error message
500
+ $error_response = sprintf( __( __METHOD__ . '(): WooCommerce MailChimp subscription failed: %s (%s)', $this->namespace ), $this->api()->get_error_message(), $this->api()->get_error_code() );
501
+
502
+ // Log
503
+ $this->log( $error_response );
504
+
505
+ // New hook for failing operations
506
+ do_action( $this->namespace_prefixed( 'subscription_failed' ), $email, array( 'list_id' => $list_id, 'order_id' => $order_id ) );
507
+
508
+ // Email admin
509
+ wp_mail( get_option( 'admin_email' ), __( 'WooCommerce MailChimp subscription failed', $this->namespace ), $error_response );
510
+ } else {
511
+ // Hook on success
512
+ do_action( $this->namespace_prefixed( 'subscription_success' ), $email, array( 'list_id' => $list_id, 'order_id' => $order_id ) );
513
+ }
514
+ }
515
+
516
+ /**
517
+ * Add the opt-in checkbox to the checkout fields (to be displayed on checkout).
518
+ *
519
+ * @since 1.1
520
+ */
521
+ function maybe_add_checkout_fields() {
522
+ if ( $this->is_valid() ) {
523
+ if ( $this->display_opt_in() ) {
524
+ do_action( $this->namespace_prefixed( 'before_opt_in_checkbox' ) );
525
+ echo apply_filters( $this->namespace_prefixed( 'opt_in_checkbox' ), '<p class="form-row woocommerce-mailchimp-opt-in"><label for="ss_wc_mailchimp_opt_in"><input type="checkbox" name="ss_wc_mailchimp_opt_in" id="ss_wc_mailchimp_opt_in" value="yes"' . ($this->opt_in_checkbox_default_status() == 'checked' ? ' checked="checked"' : '') . '/> ' . esc_html( $this->opt_in_label() ) . '</label></p>' . "\n", $this->opt_in_checkbox_default_status(), $this->opt_in_label() );
526
+ do_action( $this->namespace_prefixed( 'after_opt_in_checkbox' ) );
527
+ }
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Opt-in checkbox default support for WooCommerce 2.1
533
+ *
534
+ * @since 1.2.1
535
+ */
536
+ function checkbox_default_status( $input ) {
537
+ return $this->opt_in_checkbox_default_status === 'checked' ? 1 : 0;
538
+ }
539
+
540
+ /**
541
+ * When the checkout form is submitted, save opt-in value.
542
+ *
543
+ * @version 1.1
544
+ */
545
+ function maybe_save_checkout_fields( $order_id ) {
546
+ if ( $this->display_opt_in() ) {
547
+ $opt_in = isset( $_POST[ $this->namespace_prefixed( 'opt_in' ) ] ) ? 'yes' : 'no';
548
+
549
+ update_post_meta( $order_id, $this->namespace_prefixed( 'opt_in' ), $opt_in );
550
+ }
551
+ }
552
+
553
+ /**
554
+ * Helper log function for debugging
555
+ *
556
+ * @since 1.2.2
557
+ */
558
+ private function log( $message ) {
559
+ if ( $this->debug_enabled() ) {
560
+ $logger = new WC_Logger();
561
+
562
+ if ( is_array( $message ) || is_object( $message ) ) {
563
+ $logger->add( 'woocommerce-mailchimp', print_r( $message, true ) );
564
+ }
565
+ else {
566
+ $logger->add( 'woocommerce-mailchimp', $message );
567
+ }
568
+ }
569
+ }
570
+
571
+ } //end class SS_WC_MailChimp_Handler
572
+
573
+ } //end if ( ! class_exists( 'SS_WC_MailChimp_Handler' ) )
includes/class-ss-wc-mailchimp-migrator.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * WooCommerce MailChimp plugin migrator class
5
+ */
6
+ final class SS_WC_MailChimp_Migrator {
7
+
8
+ const VERSION_KEY = 'ss_wc_mailchimp_version';
9
+
10
+ const VERSIONS = array(
11
+ '1.3.X',
12
+ '2.0',
13
+ );
14
+
15
+ public static function migrate( $target_version ) {
16
+
17
+ $current_version = get_option( self::VERSION_KEY );
18
+ if ( ! $current_version ) {
19
+ $current_version = '1.3.X';
20
+ }
21
+
22
+ if ( $current_version !== $target_version ) {
23
+
24
+ // error_log( 'Need to migrate from ' . $current_version . ' to ' . $target_version );
25
+
26
+ require_once( 'migrations/class-ss-wc-migration.php' );
27
+
28
+ $start = array_search( $current_version, self::VERSIONS );
29
+
30
+ // error_log( 'Starting at migration ' . $start );
31
+
32
+ for ($start; $start < count(self::VERSIONS) - 1; $start++) {
33
+ $next = $start + 1;
34
+ $current_version = self::VERSIONS[$start];
35
+ $target_version = self::VERSIONS[$next];
36
+
37
+ // error_log( 'Migrating from ' . $current_version . ' to ' . $target_version );
38
+ //
39
+ if ( file_exists( SS_WC_MAILCHIMP_DIR . "includes/migrations/class-ss-wc-migration-from-$current_version-to-$target_version.php" ) ) {
40
+
41
+ require_once( SS_WC_MAILCHIMP_DIR . "includes/migrations/class-ss-wc-migration-from-$current_version-to-$target_version.php" );
42
+
43
+ $migration_name = 'SS_WC_MailChimp_Migration_From_'. self::clean_version( $current_version ) .'_To_'. self::clean_version( $target_version );
44
+
45
+ $migration = new $migration_name( $current_version, $target_version );
46
+ if ( $migration->up() ) {
47
+ // Update the current plugin version
48
+ update_option( self::VERSION_KEY, $target_version );
49
+ }
50
+
51
+ }
52
+ }
53
+
54
+ }
55
+
56
+ }
57
+
58
+ private static function clean_version( $version ) {
59
+ return str_replace( '.', '', $version );
60
+ }
61
+
62
+ }
includes/class-ss-wc-mailchimp-plugin.php ADDED
@@ -0,0 +1,267 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * WooCommerce MailChimp plugin main class
5
+ */
6
+ final class SS_WC_MailChimp_Plugin {
7
+
8
+ private static $_instance;
9
+
10
+ private static $version = '2.0';
11
+
12
+ public static function version() {
13
+ return self::$version;
14
+ // $plugin_data = get_plugin_data( SS_WC_MAILCHIMP_FILE );
15
+ // $plugin_version = $plugin_data['Version'];
16
+ // return $plugin_version;
17
+ }
18
+
19
+ /**
20
+ * Singleton instance
21
+ *
22
+ * @return SS_WC_MailChimp_Plugin SS_WC_MailChimp_Plugin object
23
+ */
24
+ public static function get_instance() {
25
+
26
+ if ( empty( self::$_instance ) ) {
27
+ self::$_instance = new self;
28
+ }
29
+
30
+ return self::$_instance;
31
+ }
32
+
33
+ /**
34
+ * Constructor
35
+ *
36
+ * @access public
37
+ * @return void
38
+ */
39
+ public function __construct() {
40
+
41
+ $this->id = 'mailchimp';
42
+ $this->namespace = 'ss_wc_' . $this->id;
43
+ $this->label = __( 'MailChimp', $this->namespace );
44
+
45
+ $this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=' . $this->id );
46
+
47
+ $this->define_constants();
48
+
49
+ $this->includes();
50
+
51
+ $this->init();
52
+
53
+ $this->add_hooks();
54
+
55
+ do_action( 'ss_wc_mailchimp_loaded' );
56
+
57
+ } //end function __construct
58
+
59
+ /**
60
+ * Define Plugin Constants.
61
+ */
62
+ private function define_constants() {
63
+
64
+ global $woocommerce;
65
+
66
+ $settings_url = admin_url( 'admin.php?page=woocommerce_settings&tab=integration&section=mailchimp' );
67
+
68
+ if ( $woocommerce->version >= '2.1' ) {
69
+ $settings_url = admin_url( 'admin.php?page=wc-settings&tab=integration&section=mailchimp' );
70
+ }
71
+
72
+ $this->define( 'SS_WC_MAILCHIMP_SETTINGS_URL', $this->settings_url );
73
+
74
+ $this->define( 'SS_WC_MAILCHIMP_PLUGIN_VERSION', self::version() );
75
+
76
+ }
77
+
78
+ /**
79
+ * Define constant if not already set.
80
+ *
81
+ * @param string $name
82
+ * @param string|bool $value
83
+ */
84
+ private function define( $name, $value ) {
85
+ if ( ! defined( $name ) ) {
86
+ define( $name, $value );
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Include required core plugin files
92
+ */
93
+ public function includes() {
94
+
95
+ if ( ! class_exists( 'SS_WC_MailChimp_Container' ) ) {
96
+ require_once( 'class-ss-wc-mailchimp-container.php' );
97
+ }
98
+
99
+ if ( ! function_exists( 'ss_wc_mailchimp' ) ) {
100
+ require_once( 'functions.php' );
101
+ }
102
+
103
+ require_once( 'lib/class-ss-system-info.php' );
104
+
105
+ require_once( 'class-ss-wc-mailchimp-api.php' );
106
+
107
+ require_once( 'class-ss-wc-mailchimp.php' );
108
+
109
+ require_once( 'class-ss-wc-mailchimp-handler.php' );
110
+
111
+ }
112
+
113
+ /**
114
+ * Initialize the plugin
115
+ * @return void
116
+ */
117
+ private function init() {
118
+
119
+ if ( ! class_exists( 'WC_Integration' ) )
120
+ return;
121
+
122
+ global $ss_wc_mailchimp;
123
+
124
+ /**
125
+ * @global SS_WC_MailChimp_Container $GLOBALS['ss_wc_mailchimp']
126
+ * @name $mc4wp
127
+ */
128
+ $ss_wc_mailchimp = ss_wc_mailchimp();
129
+ $ss_wc_mailchimp['mailchimp'] = 'ss_wc_mailchimp_get_mailchimp';
130
+ $ss_wc_mailchimp['api'] = 'ss_wc_mailchimp_get_api';
131
+
132
+ // Set up localization.
133
+ $this->load_plugin_textdomain();
134
+
135
+ }
136
+
137
+ /**
138
+ * Load Localization files.
139
+ *
140
+ * Note: the first-loaded translation file overrides any following ones if the same translation is present.
141
+ *
142
+ * Locales found in:
143
+ * - WP_LANG_DIR/woocommerce-mailchimp/woocommerce-mailchimp-LOCALE.mo
144
+ * - WP_CONTENT_DIR/plugins/woocommerce-mailchimp/languages/woocommerce-mailchimp-LOCALE.mo
145
+ */
146
+ public function load_plugin_textdomain() {
147
+ $locale = apply_filters( 'plugin_locale', get_locale(), $this->namespace );
148
+
149
+ load_textdomain( $this->namespace, WP_LANG_DIR . '/woocommerce-mailchimp/woocommerce-mailchimp-' . $locale . '.mo' );
150
+ load_plugin_textdomain( $this->namespace, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
151
+ }
152
+
153
+ /**
154
+ * Add plugin hooks
155
+ */
156
+ private function add_hooks() {
157
+ // Add the "Settings" links on the Plugins administration screen
158
+ if ( is_admin() ) {
159
+ add_filter( 'plugin_action_links_' . plugin_basename( SS_WC_MAILCHIMP_FILE ), array( $this, 'action_links' ) );
160
+ // add_filter( 'woocommerce_integrations', array( $this, 'add_mailchimp_integration' ) );
161
+ add_filter( 'woocommerce_get_settings_pages', array( $this, 'add_mailchimp_settings' ) );
162
+
163
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts') );
164
+
165
+ }
166
+
167
+ SS_WC_MailChimp_Handler::get_instance();
168
+ }
169
+
170
+ /**
171
+ * Add Settings link to plugins list
172
+ *
173
+ * @param array $links Plugin links
174
+ * @return array Modified plugin links
175
+ */
176
+ public function action_links( $links ) {
177
+ $plugin_links = array(
178
+ '<a href="' . SS_WC_MAILCHIMP_SETTINGS_URL . '">' . __( 'Settings', $this->namespace ) . '</a>',
179
+ );
180
+
181
+ return array_merge( $plugin_links, $links );
182
+ }
183
+
184
+ /**
185
+ * Add the Integration to WooCommerce
186
+ */
187
+ public function add_mailchimp_integration( $integrations ) {
188
+
189
+ require_once( 'class-ss-wc-integration-mailchimp.php' );
190
+
191
+ $integrations[] = 'SS_WC_Integration_MailChimp';
192
+
193
+ return $integrations;
194
+ }
195
+
196
+ /**
197
+ * Add the MailChimp settings tab to WooCommerce
198
+ */
199
+ function add_mailchimp_settings( $settings ) {
200
+
201
+ $settings[] = require_once( 'class-ss-wc-settings-mailchimp.php' );
202
+
203
+ return $settings;
204
+
205
+ } //end function add_mailchimp_settings
206
+
207
+ /**
208
+ * Load scripts required for admin
209
+ *
210
+ * @access public
211
+ * @return void
212
+ */
213
+ public function enqueue_scripts() {
214
+
215
+ // Plugin scripts and styles
216
+ wp_register_script( 'woocommerce-mailchimp-admin', SS_WC_MAILCHIMP_PLUGIN_URL . '/assets/js/woocommerce-mailchimp-admin.js', array( 'jquery' ), self::version() );
217
+ wp_register_style( 'woocommerce-mailchimp', SS_WC_MAILCHIMP_PLUGIN_URL . '/assets/css/style.css', array(), self::version() );
218
+
219
+ // Localize javascript messages
220
+ $translation_array = array(
221
+ 'connecting_to_mailchimp' => __( 'Connecting to MailChimp', $this->namespace ),
222
+ 'error_loading_lists' => __( 'Error loading lists. Please check your api key.', $this->namespace ),
223
+ 'error_loading_groups' => __( 'Error loading groups. Please check your MailChimp Interest Groups for the selected list.', $this->namespace ),
224
+ 'select_groups_placeholder' => __( 'Select one or more groups (optional)', $this->namespace ),
225
+ 'interest_groups_not_enabled' => __( 'This list does not have interest groups enabled', $this->namespace ),
226
+ );
227
+ wp_localize_script( 'woocommerce-mailchimp-admin', 'SS_WC_MailChimp_Messages', $translation_array );
228
+
229
+ // Scripts
230
+ wp_enqueue_script( 'woocommerce-mailchimp-admin' );
231
+
232
+ // Styles
233
+ wp_enqueue_style( 'woocommerce-mailchimp' );
234
+
235
+ } //end function enqueue_scripts
236
+
237
+ /**
238
+ * Plugin activate function.
239
+ *
240
+ * @access public
241
+ * @static
242
+ * @param mixed $network_wide
243
+ * @return void
244
+ */
245
+ public static function activate( $network_wide = false ) {
246
+
247
+ require_once( 'class-ss-wc-mailchimp-migrator.php' );
248
+
249
+ SS_WC_MailChimp_Migrator::migrate( self::version() );
250
+
251
+ } //end function activate
252
+
253
+ /**
254
+ * Plugin deactivate function.
255
+ *
256
+ * @access public
257
+ * @static
258
+ * @param mixed $network_wide
259
+ * @return void
260
+ */
261
+ public static function deactivate( $network_wide ) {
262
+
263
+
264
+
265
+ } //end function deactivate
266
+
267
+ } //end final class SS_WC_MailChimp_Plugin
includes/class-ss-wc-mailchimp.php ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
+
5
+ /**
6
+ * Minimal MailChimp helper
7
+ *
8
+ * @class SS_WC_MailChimp
9
+ * @version 2.0
10
+ * @package WooCommerce MailChimp
11
+ * @author Saint Systems
12
+ */
13
+ class SS_WC_MailChimp {
14
+
15
+ /**
16
+ * @var SS_WC_MailChimp_API
17
+ */
18
+ public $api;
19
+
20
+ /**
21
+ * Create a new instance
22
+ * @param string $api_key MailChimp API key
23
+ */
24
+ function __construct( $api_key, $debug = false ) {
25
+
26
+ $this->debug = $debug;
27
+
28
+ require_once( SS_WC_MAILCHIMP_DIR . 'includes/class-ss-wc-mailchimp-api.php' );
29
+ $this->api = new SS_WC_MailChimp_API( $api_key, $debug );
30
+
31
+ } //end function __construct
32
+
33
+ /**
34
+ * Get list
35
+ *
36
+ * @access public
37
+ * @return mixed
38
+ */
39
+ public function get_lists( $args = array() ) {
40
+
41
+ if ( ! $results = get_transient( 'sswcmc_lists' ) ) {
42
+
43
+ $resource = 'lists';
44
+
45
+ if ( ! array_key_exists( 'count', $args ) ) {
46
+ $args['count'] = 100;
47
+ }
48
+
49
+ $response = $this->api->get( $resource, $args );
50
+
51
+ if ( ! $response ) {
52
+ return false;
53
+ }
54
+
55
+ $lists = $response['lists'];
56
+
57
+ $results = array();
58
+
59
+ foreach ( $lists as $list ) {
60
+
61
+ $results[ (string)$list['id'] ] = $list['name'];
62
+
63
+ }
64
+
65
+ set_transient( 'sswcmc_lists', $results, 60*15*1 );
66
+
67
+ }
68
+
69
+ return $results;
70
+
71
+ } //end function get_lists
72
+
73
+ public function subscribe( $list_id, $email_address, $email_type, $merge_fields, $interests, $double_optin ) {
74
+
75
+ $args = array(
76
+ 'email_address' => $email_address,
77
+ 'status' => $double_optin ? 'pending' : 'subscribed',
78
+ 'email_type' => $email_type,
79
+ 'merge_fields' => $merge_fields,
80
+ );
81
+
82
+ if ( is_array( $interests ) && !empty( $interests ) ) {
83
+ $args['interests'] = $interests;
84
+ }
85
+
86
+ $subscriber_hash = md5( strtolower( $email_address ) );
87
+
88
+ $resource = "lists/$list_id/members/$subscriber_hash";
89
+
90
+ $response = $this->api->put( $resource, $args );
91
+
92
+ if ( ! $response ) {
93
+ return false;
94
+ }
95
+
96
+ return $response;
97
+
98
+ } //end function subscribe
99
+
100
+ /**
101
+ * Get merge fields
102
+ *
103
+ * @access public
104
+ * @param string $list_id
105
+ * @return mixed
106
+ */
107
+ public function get_merge_fields( $list_id ) {
108
+
109
+ $resource = "lists/$list_id/merge-fields";
110
+
111
+ $response = $this->api->get( $resource );
112
+
113
+ if ( ! $response ) {
114
+ return false;
115
+ }
116
+
117
+ $merge_fields = $response['merge_fields'];
118
+
119
+ $results = array();
120
+
121
+ foreach ( $merge_fields as $merge_field ) {
122
+
123
+ $results[ $merge_field['id'] ] = $merge_field['tag'];
124
+
125
+ }
126
+
127
+ return $results;
128
+
129
+ } //end function get_merge_fields
130
+
131
+ /**
132
+ * Get interest categories
133
+ *
134
+ * @access public
135
+ * @param string $list_id
136
+ * @return mixed
137
+ */
138
+ public function get_interest_categories( $list_id ) {
139
+
140
+ if ( ! $results = get_transient( "sswcmc_{$list_id}_interest_categories" ) ) {
141
+
142
+ $resource = "lists/$list_id/interest-categories";
143
+
144
+ $response = $this->api->get( $resource );
145
+
146
+ if ( ! $response ) {
147
+ return false;
148
+ }
149
+
150
+ $categories = $response['categories'];
151
+
152
+ $results = array();
153
+
154
+ foreach ( $categories as $category ) {
155
+
156
+ $results[ $category['id'] ] = $category['title'];
157
+
158
+ }
159
+
160
+ set_transient( "sswcmc_{$list_id}_interest_categories", $results, 60*15*1 );
161
+
162
+ }
163
+
164
+ return $results;
165
+
166
+ } //end function get_interest_categories
167
+
168
+ /**
169
+ * Get interest category interests
170
+ *
171
+ * @access public
172
+ * @param string $list_id
173
+ * * @param string $interest_category_id
174
+ * @return mixed
175
+ */
176
+ public function get_interest_category_interests( $list_id, $interest_category_id ) {
177
+
178
+ if ( ! $results = get_transient( "sswcmc_{$list_id}_{$interest_category_id }_interests" ) ) {
179
+
180
+ $resource = "lists/$list_id/interest-categories/$interest_category_id/interests";
181
+
182
+ $response = $this->api->get( $resource );
183
+
184
+ if ( ! $response ) {
185
+ return false;
186
+ }
187
+
188
+ $interests = $response['interests'];
189
+
190
+ $results = array();
191
+
192
+ foreach ( $interests as $interest ) {
193
+
194
+ $results[ $interest['id'] ] = $interest['name'];
195
+
196
+ }
197
+
198
+ set_transient( "sswcmc_{$list_id}_{$interest_category_id }_interests", $results, 60*15*1 );
199
+
200
+ }
201
+
202
+ return $results;
203
+
204
+ } //end function get_interest_category_interests
205
+
206
+ /**
207
+ * Get interest categories with interests
208
+ *
209
+ * @access public
210
+ * @param string $list_id
211
+ * @return mixed
212
+ */
213
+ public function get_interest_categories_with_interests( $list_id ) {
214
+
215
+ $categories = $this->get_interest_categories( $list_id );
216
+
217
+ if ( ! $categories ) {
218
+ return false;
219
+ }
220
+
221
+ $results = array();
222
+
223
+ foreach ( $categories as $category_id => $category ) {
224
+
225
+ $interests = $this->get_interest_category_interests( $list_id, $category_id );
226
+
227
+ if ( ! $interests ) {
228
+ return false;
229
+ }
230
+
231
+ foreach ( $interests as $interest_id => $interest ) {
232
+
233
+ $results[ $interest_id ] = $category . ': ' . $interest;
234
+
235
+ }
236
+
237
+ }
238
+
239
+ return $results;
240
+
241
+ } //end function get_interest_categories_with_interests
242
+
243
+ /**
244
+ * Returns error code from error property
245
+ * @return string error code
246
+ */
247
+ public function get_error_code() {
248
+
249
+ return $this->api->get_error_code();
250
+
251
+ } //end get_error_code
252
+
253
+ /**
254
+ * Returns error message from error property
255
+ * @return string error message
256
+ */
257
+ public function get_error_message() {
258
+
259
+ return $this->api->get_error_message();
260
+
261
+ } //end get_error_message
262
+
263
+ } //end class SS_WC_MailChimp
includes/class-ss-wc-settings-mailchimp.php ADDED
@@ -0,0 +1,768 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WooCommerce MailChimp Settings
4
+ *
5
+ * @author Saint Systems
6
+ * @package WooCommerce MailChimp
7
+ * @version 2.0
8
+ */
9
+
10
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
11
+
12
+ if ( ! class_exists( 'SS_WC_Settings_MailChimp' ) ) {
13
+
14
+ /**
15
+ * @class SS_WC_Settings_MailChimp
16
+ * @extends WC_Settings_Page
17
+ */
18
+ class SS_WC_Settings_MailChimp extends WC_Settings_Page {
19
+
20
+ private static $_instance;
21
+
22
+ /**
23
+ * Singleton instance
24
+ *
25
+ * @return SS_WC_Settings_MailChimp SS_WC_Settings_MailChimp object
26
+ */
27
+ public static function get_instance() {
28
+
29
+ if ( empty( self::$_instance ) ) {
30
+ self::$_instance = new self;
31
+ }
32
+
33
+ return self::$_instance;
34
+ }
35
+
36
+ /**
37
+ * Instance of the API class.
38
+ * @var Object
39
+ */
40
+ private static $api_instance = null;
41
+
42
+ /**
43
+ * Constructor
44
+ *
45
+ * @access public
46
+ * @return void
47
+ */
48
+ public function __construct() {
49
+
50
+ $this->id = 'mailchimp';
51
+ $this->namespace = 'ss_wc_' . $this->id;
52
+ $this->label = __( 'MailChimp', $this->namespace );
53
+
54
+ $this->init();
55
+
56
+ $this->register_hooks();
57
+
58
+ } //end function __construct
59
+
60
+ /**
61
+ * api_key function.
62
+ * @return string MailChimp API Key
63
+ */
64
+ public function api_key() {
65
+ return $this->get_option( 'api_key' );
66
+ }
67
+
68
+ /**
69
+ * is_enabled function.
70
+ *
71
+ * @access public
72
+ * @return boolean
73
+ */
74
+ public function is_enabled() {
75
+ return 'yes' === $this->get_option( 'enabled' );
76
+ }
77
+
78
+ /**
79
+ * occurs function
80
+ * @return string
81
+ */
82
+ public function occurs() {
83
+ return $this->get_option( 'occurs' );
84
+ }
85
+
86
+ /**
87
+ * list function.
88
+ *
89
+ * @access public
90
+ * @return string MailChimp list ID
91
+ */
92
+ public function list() {
93
+ return $this->get_option( 'list' );
94
+ }
95
+
96
+ /**
97
+ * double_optin function.
98
+ *
99
+ * @access public
100
+ * @return boolean
101
+ */
102
+ public function double_optin() {
103
+ return 'yes' === $this->get_option( 'double_optin' );
104
+ }
105
+
106
+ /**
107
+ * display_opt_in function.
108
+ *
109
+ * @access public
110
+ * @return boolean
111
+ */
112
+ public function display_opt_in() {
113
+ return 'yes' === $this->get_option( 'display_opt_in' );
114
+ }
115
+
116
+ /**
117
+ * opt_in_label function.
118
+ *
119
+ * @access public
120
+ * @return string
121
+ */
122
+ public function opt_in_label() {
123
+ return $this->get_option( 'opt_in_label' );
124
+ }
125
+
126
+ /**
127
+ * opt_in_checkbox_default_status function.
128
+ *
129
+ * @access public
130
+ * @return string
131
+ */
132
+ public function opt_in_checkbox_default_status() {
133
+ return $this->get_option( 'opt_in_checkbox_default_status' );
134
+ }
135
+
136
+ /**
137
+ * opt_in_checkbox_display_location function.
138
+ *
139
+ * @access public
140
+ * @return string
141
+ */
142
+ public function opt_in_checkbox_display_location() {
143
+ return $this->get_option( 'opt_in_checkbox_display_location' );
144
+ }
145
+
146
+ /**
147
+ * interests function.
148
+ *
149
+ * @access public
150
+ * @return array
151
+ */
152
+ public function interest_groups() {
153
+ return $this->get_option( 'interest_groups' );
154
+ }
155
+
156
+ /**
157
+ * has_list function.
158
+ *
159
+ * @access public
160
+ * @return boolean
161
+ */
162
+ public function has_list() {
163
+ if ( $this->list() ) {
164
+ return true;
165
+ }
166
+ return false;
167
+ }
168
+
169
+ /**
170
+ * has_api_key function.
171
+ *
172
+ * @access public
173
+ * @return boolean
174
+ */
175
+ public function has_api_key() {
176
+ return !empty( $this->api_key() );
177
+ }
178
+
179
+ /**
180
+ * is_valid function.
181
+ *
182
+ * @access public
183
+ * @return boolean
184
+ */
185
+ public function is_valid() {
186
+ return $this->is_enabled() && $this->has_api_key() && $this->has_list();
187
+ }
188
+
189
+ /**
190
+ * debug_enabled function.
191
+ *
192
+ * @access public
193
+ * @return boolean
194
+ */
195
+ public function debug_enabled() {
196
+ return 'yes' === $this->get_option( 'debug' );
197
+ }
198
+
199
+ /**
200
+ * Check if the user has enabled the plugin functionality, but hasn't provided an api key
201
+ **/
202
+ function checks() {
203
+ // Check required fields
204
+ if ( $this->is_enabled() && ! $this->has_api_key() ) {
205
+ // Show notice
206
+ echo $this->get_message( sprintf( '%s <a href="%s">%s</a>.',
207
+ __( 'WooCommerce MailChimp error: Plugin is enabled but no api key provided. Please enter your api key', $this->namespace ),
208
+ WOOCOMMERCE_MAILCHIMP_SETTINGS_URL,
209
+ __( 'here', $this->namespace )
210
+ )
211
+ );
212
+ }
213
+ }
214
+
215
+ public function init() {
216
+
217
+ $this->api_key = $this->get_option( 'api_key' );
218
+
219
+ $this->enabled = $this->get_option( 'enabled' );
220
+
221
+ }
222
+
223
+ public function get_option( $option_suffix ) {
224
+
225
+ return get_option( $this->namespace_prefixed( $option_suffix ) );
226
+
227
+ }
228
+
229
+ /**
230
+ * Register plugin hooks
231
+ *
232
+ * @access public
233
+ * @return void
234
+ */
235
+ public function register_hooks() {
236
+
237
+ // Hook in to add the MailChimp tab
238
+ add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
239
+ add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
240
+ add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
241
+ add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
242
+ add_action( 'woocommerce_settings_saved', array( $this, 'init' ) );
243
+
244
+ // Hooks
245
+ add_action( 'admin_notices', array( $this, 'checks' ) );
246
+ add_action( 'woocommerce_admin_field_sysinfo', array( $this, 'sysinfo_field' ), 10, 1 );
247
+
248
+ } //end function ensure_tab
249
+
250
+ // /**
251
+ // * Get sections
252
+ // *
253
+ // * @return array
254
+ // */
255
+ public function get_sections() {
256
+
257
+ $sections = array(
258
+ '' => __( 'General', $this->namespace ),
259
+ // 'checkout' => __( 'Checkout', $this->namespace ),
260
+ // //'widget' => __( 'Widget', $this->namespace ),
261
+ // 'shortcode' => __( 'ShortCode', $this->namespace ),
262
+ 'troubleshooting' => __( 'Troubleshooting', $this->namespace ),
263
+ );
264
+
265
+ return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
266
+ }
267
+
268
+ /**
269
+ * Output the settings
270
+ */
271
+ public function output() {
272
+
273
+ global $current_section;
274
+
275
+ $settings = $this->get_settings( $current_section );
276
+
277
+ WC_Admin_Settings::output_fields( $settings );
278
+
279
+ $this->wc_enqueue_js("
280
+ (function($){
281
+
282
+ $(document).ready(function() {
283
+ SS_WC_MailChimp.init();
284
+ });
285
+
286
+ })(jQuery);
287
+ ");
288
+
289
+ }
290
+
291
+ /**
292
+ * Save settings
293
+ */
294
+ public function save() {
295
+ global $current_section;
296
+
297
+ $settings = $this->get_settings( $current_section );
298
+ WC_Admin_Settings::save_fields( $settings );
299
+ }
300
+
301
+ /**
302
+ * Get settings array
303
+ *
304
+ * @return array
305
+ */
306
+ public function get_settings( $current_section = '' ) {
307
+
308
+ if ( '' === $current_section ) {
309
+
310
+ $settings = array(
311
+ array(
312
+ 'title' => __( 'MailChimp', $this->namespace ),
313
+ 'type' => 'title',
314
+ 'desc' => __( 'Enter your MailChimp settings below to control how WooCommerce integrates with your MailChimp account.', $this->namespace ),
315
+ 'id' => 'general_options',
316
+ ),
317
+ );
318
+
319
+ $settings[] = array(
320
+ 'id' => $this->namespace_prefixed( 'api_key' ),
321
+ 'title' => __( 'API Key', $this->namespace ),
322
+ 'type' => 'text',
323
+ 'desc' => sprintf( '<br/><a href="https://admin.mailchimp.com/account/api/" target="_blank">%s</a> %s',
324
+ __( 'Login to MailChimp', $this->namespace ),
325
+ __( 'to look up your api key.', $this->namespace )
326
+ ),
327
+ 'default' => '',
328
+ 'css' => 'min-width:350px;',
329
+ 'desc_tip' => 'Your API Key is required for the plugin to communicate with your MailChimp account.',
330
+ );
331
+
332
+ $mailchimp_lists = $this->get_lists();
333
+
334
+ if ( ! $this->has_api_key() || $mailchimp_lists === false ) {
335
+ $settings[] = array( 'type' => 'sectionend', 'id' => 'general_options' );
336
+ }
337
+ // if ( !$this->has_api_key() ) {
338
+ // $form_fields['api_key']['description'] = sprintf( '%s <strong>%s</strong> %s.<br/>',
339
+ // __( 'Paste your API key above and click', $this->namespace ),
340
+ // __( 'Save changes', $this->namespace ),
341
+ // __( 'below', $this->namespace )
342
+ // ) . $form_fields['api_key']['description'];
343
+ // }
344
+
345
+ //if ( is_admin() && ! is_ajax() ) {
346
+
347
+ // if ( $this->has_api_key() && $mailchimp_lists !== false ) {
348
+
349
+ if ( $this->has_api_key() && $this->has_list() ) {
350
+ $interest_groups = $this->get_interest_groups();
351
+ } else {
352
+ $interest_groups = array();
353
+ }
354
+
355
+ $settings[] = array(
356
+ 'id' => $this->namespace_prefixed( 'enabled' ),
357
+ 'title' => __( 'Enable/Disable', $this->namespace ),
358
+ 'label' => __( 'Enable MailChimp Integration', $this->namespace ),
359
+ 'type' => 'checkbox',
360
+ 'desc' => __( 'Enable/disable the plugin functionality.', $this->namespace ),
361
+ 'default' => 'yes',
362
+ );
363
+
364
+ $settings[] = array(
365
+ 'id' => $this->namespace_prefixed( 'list' ),
366
+ 'title' => __( 'Main List', $this->namespace ),
367
+ 'type' => 'select',
368
+ 'desc' => __( 'All customers will be added to this list.', $this->namespace ),
369
+ 'default' => '',
370
+ 'options' => $mailchimp_lists,
371
+ 'class' => 'wc-enhanced-select',
372
+ 'css' => 'min-width: 350px;',
373
+ // 'custom_attributes' => array(
374
+ // 'onchange' => 'form.submit()',
375
+ // ),
376
+ 'desc_tip' => true,
377
+ );
378
+ // if ( array_key_exists( 'no_lists', $mailchimp_lists ) ) {
379
+ // $form_fields['list']['description'] = sprintf( __( 'There are no lists in your MailChimp account. <a href="%s" target="_blank">Click here</a> to create one.', $this->namespace ), 'https://admin.mailchimp.com/lists/new-list/' );
380
+ // }
381
+
382
+ $settings[] = array(
383
+ 'id' => $this->namespace_prefixed( 'interest_groups' ),
384
+ 'title' => __( 'Interest Groups', $this->namespace ),
385
+ 'type' => 'multiselect',
386
+ 'desc' => __( 'Optional: Interest groups to assign to subscribers.', $this->namespace ),
387
+ 'default' => '',
388
+ 'options' => $interest_groups,
389
+ 'class' => 'wc-enhanced-select',
390
+ 'custom_attributes' => array(
391
+ 'placeholder' => __( 'Select interest groups...', $this->namespace ),
392
+ ),
393
+ 'css' => 'min-width: 350px;',
394
+ 'desc_tip' => true,
395
+ );
396
+
397
+ // if ( is_array( $interest_groups ) && count( $interest_groups ) == 0 ) {
398
+ // // $form_fields['interest_groups']['description'] = __( 'Optional: Interest groups to assign to subscribers.', $this->namespace );
399
+ // $form_fields['interest_groups']['custom_attributes']['placeholder'] = __( 'This list has no interest groups.', $this->namespace );
400
+ // $form_fields['interest_groups']['custom_attributes']['disabled'] = 'disabled';
401
+ // } elseif ( !$this->has_list() ) {
402
+ // $form_fields['interest_groups']['custom_attributes']['placeholder'] = __( 'Select a list to see interests', $this->namespace );
403
+ // $form_fields['interest_groups']['custom_attributes']['disabled'] = 'disabled';
404
+ // }
405
+ $settings[] = array( 'type' => 'sectionend', 'id' => 'general_options' );
406
+
407
+ $settings[] = array(
408
+ 'title' => __( 'Checkout Settings', $this->namespace ),
409
+ 'type' => 'title',
410
+ 'desc' => '',
411
+ 'id' => 'checkout_settings'
412
+ );
413
+
414
+ $settings[] = array(
415
+ 'id' => $this->namespace_prefixed( 'occurs' ),
416
+ 'title' => __( 'Subscribe Event', $this->namespace ),
417
+ 'type' => 'select',
418
+ 'desc' => __( 'Choose whether to subscribe customers as soon as an order is placed or after the order is processing or completed.', $this->namespace ),
419
+ 'class' => 'wc-enhanced-select',
420
+ 'default' => 'pending',
421
+ 'options' => array(
422
+ 'pending' => __( 'Order Created', $this->namespace ),
423
+ 'processing' => __( 'Order Processing', $this->namespace ),
424
+ 'completed' => __( 'Order Completed', $this->namespace ),
425
+ ),
426
+ 'desc_tip' => true,
427
+ );
428
+
429
+ $settings[] = array(
430
+ 'id' => $this->namespace_prefixed( 'double_optin' ),
431
+ 'title' => __( 'Double Opt-In', $this->namespace ),
432
+ 'desc' => __( 'Enable Double Opt-In', $this->namespace ),
433
+ 'type' => 'checkbox',
434
+ 'default' => 'no',
435
+ 'desc_tip' => __( 'If enabled, customers will receive an email prompting them to confirm their subscription to the list above.', $this->namespace ),
436
+ );
437
+
438
+ // $settings[] = array(
439
+ // 'id' => $this->namespace_prefixed( 'display_opt_in' ),
440
+ // 'title' => __( 'Display Opt-In Field', $this->namespace ),
441
+ // 'desc' => __( 'Display an Opt-In Field on Checkout', $this->namespace ),
442
+ // 'type' => 'checkbox',
443
+ // 'desc_tip' => __( '<p>Choose <strong>Automatically</strong> to subscribe customers silently upon checkout. Caution, this is without the customer\'s consent.</p> <p>Choose <strong>Ask for permission</strong> to show an "Opt-in" checkbox during checkout. Customers will only be subscribed to the list above if they opt-in.' , $this->namespace ),
444
+ // 'default' => no,
445
+ // );
446
+
447
+ $settings[] = array(
448
+ 'id' => $this->namespace_prefixed( 'display_opt_in' ),
449
+ 'title' => __( 'Subscribe Customers', $this->namespace ),
450
+ 'desc' => __( '<p>Choose <strong>Ask for permission</strong> to show an "Opt-in" checkbox during checkout. Customers will only be subscribed to the list above if they opt-in. <p>Choose <strong>Automatically</strong> to subscribe customers silently upon checkout. Caution, this is without the customer\'s consent.</p>', $this->namespace ),
451
+ 'type' => 'select',
452
+ 'css' => 'min-width:300px;',
453
+ 'class' => 'wc-enhanced-select',
454
+ 'desc_tip' => true,
455
+ 'default' => 'yes',
456
+ 'options' => array(
457
+ // '0' => __( 'Disabled', $this->namespace ),
458
+ 'yes' => __( 'Ask for permission', $this->namespace ),
459
+ 'no' => __( 'Automatically', $this->namespace ),
460
+ )
461
+ );
462
+
463
+ // $settings[] = array(
464
+ // 'id' => $this->namespace_prefixed( 'display_opt_in' ),
465
+ // 'title' => __( 'Display Opt-In Field', $this->namespace ),
466
+ // 'label' => __( 'Display an Opt-In Field on Checkout', $this->namespace ),
467
+ // 'type' => 'checkbox',
468
+ // 'desc' => __( 'If enabled, customers will be presented with a "Opt-in" checkbox during checkout and will only be added to the list above if they opt-in.', $this->namespace ),
469
+ // 'default' => 'no',
470
+ // );
471
+
472
+ $settings[] = array(
473
+ 'id' => $this->namespace_prefixed( 'opt_in_label' ),
474
+ 'title' => __( 'Opt-In Field Label', $this->namespace ),
475
+ 'type' => 'text',
476
+ 'desc' => __( 'Optional: customize the label displayed next to the opt-in checkbox.', $this->namespace ),
477
+ 'default' => __( 'Subscribe to our newsletter', $this->namespace ),
478
+ 'css' => 'min-width:350px;',
479
+ 'desc_tip' => true,
480
+ );
481
+
482
+ $settings[] = array(
483
+ 'id' => $this->namespace_prefixed( 'opt_in_checkbox_default_status' ),
484
+ 'title' => __( 'Opt-In Checkbox Default', $this->namespace ),
485
+ 'type' => 'select',
486
+ 'desc' => __( 'The default state of the opt-in checkbox.', $this->namespace ),
487
+ 'class' => 'wc-enhanced-select',
488
+ 'default' => 'checked',
489
+ 'options' => array(
490
+ 'checked' => __( 'Checked', $this->namespace ),
491
+ 'unchecked' => __( 'Unchecked', $this->namespace )
492
+ ),
493
+ 'desc_tip' => true,
494
+ );
495
+
496
+ $settings[] = array(
497
+ 'id' => $this->namespace_prefixed( 'opt_in_checkbox_display_location' ),
498
+ 'title' => __( 'Opt-In Checkbox Location', $this->namespace ),
499
+ 'type' => 'select',
500
+ 'desc' => __( 'Where to display the opt-in checkbox on the checkout page.', $this->namespace ),
501
+ 'class' => 'wc-enhanced-select',
502
+ 'default' => 'woocommerce_review_order_before_submit',
503
+ 'options' => array(
504
+ 'woocommerce_checkout_before_customer_details' => __( 'Above customer details', $this->namespace ),
505
+ 'woocommerce_checkout_after_customer_details' => __( 'Below customer details', $this->namespace ),
506
+ 'woocommerce_review_order_before_submit' => __( 'Order review above submit', $this->namespace ),
507
+ 'woocommerce_review_order_after_submit' => __( 'Order review below submit', $this->namespace ),
508
+ 'woocommerce_review_order_before_order_total' => __( 'Order review above total', $this->namespace ),
509
+ 'woocommerce_checkout_billing' => __( 'Above billing details', $this->namespace ),
510
+ 'woocommerce_checkout_shipping' => __( 'Above shipping details', $this->namespace ),
511
+ 'woocommerce_after_checkout_billing_form' => __( 'Below Checkout billing form', $this->namespace ),
512
+ ),
513
+ 'desc_tip' => true,
514
+ );
515
+
516
+ $settings[] = array( 'type' => 'sectionend', 'id' => 'checkout_settings' );
517
+
518
+ // }
519
+
520
+ $settings = apply_filters( $this->namespace_prefixed( 'settings_general' ), $settings );
521
+
522
+ } elseif ( 'troubleshooting' === $current_section ) {
523
+
524
+ $label = __( 'Enable Logging', $this->namespace );
525
+
526
+ if ( defined( 'WC_LOG_DIR' ) ) {
527
+ $debug_log_url = add_query_arg( 'tab', 'logs', add_query_arg( 'page', 'wc-status', admin_url( 'admin.php' ) ) );
528
+ $debug_log_key = 'woocommerce-mailchimp-' . sanitize_file_name( wp_hash( 'woocommerce-mailchimp' ) ) . '-log';
529
+ $debug_log_url = add_query_arg( 'log_file', $debug_log_key, $debug_log_url );
530
+
531
+ $label .= ' | ' . sprintf( __( '%1$sView Log%2$s', $this->namespace ), '<a href="' . esc_url( $debug_log_url ) . '">', '</a>' );
532
+ }
533
+
534
+ $settings[] = array(
535
+ 'title' => __( 'Troubleshooting', $this->namespace ),
536
+ 'type' => 'title',
537
+ 'desc' => '',
538
+ 'id' => 'troubleshooting_settings'
539
+ );
540
+
541
+ $settings[] = array(
542
+ 'id' => $this->namespace_prefixed( 'debug' ),
543
+ 'title' => __( 'Debug Log', $this->namespace ),
544
+ 'desc' => $label,
545
+ 'type' => 'checkbox',
546
+ 'default' => 'no',
547
+ 'desc_tip' => __( 'Enable logging MailChimp API calls. Only enable for troubleshooting purposes.', $this->namespace ),
548
+ );
549
+
550
+ $settings[] = array(
551
+ 'id' => 'sysinfo',
552
+ 'title' => __( 'System Info', $this->namespace ),
553
+ 'type' => 'sysinfo',
554
+ 'desc' => __( 'Copy the information below and send it to us when reporting an issue with the plugin.<p/>', $this->namespace ),
555
+ 'desc_tip' => '',
556
+ );
557
+
558
+ $settings[] = array( 'type' => 'sectionend', 'id' => 'troubleshooting_settings' );
559
+
560
+ $settings = apply_filters( $this->namespace_prefixed( 'settings_troubleshooting' ), $settings );
561
+
562
+ }
563
+
564
+ //}
565
+
566
+ return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $current_section );
567
+
568
+ } //end function get_settings
569
+
570
+ private function namespace_prefixed( $value ) {
571
+ return $this->namespace . '_' . $value;
572
+ }
573
+
574
+ /**
575
+ * WooCommerce 2.1 support for wc_enqueue_js
576
+ *
577
+ * @since 1.2.1
578
+ *
579
+ * @access private
580
+ * @param string $code
581
+ * @return void
582
+ */
583
+ private function wc_enqueue_js( $code ) {
584
+ if ( function_exists( 'wc_enqueue_js' ) ) {
585
+ wc_enqueue_js( $code );
586
+ } else {
587
+ global $woocommerce;
588
+ $woocommerce->add_inline_js( $code );
589
+ }
590
+ }
591
+
592
+ /**
593
+ * Get message
594
+ * @return string Error
595
+ */
596
+ private function get_message( $message, $type = 'error' ) {
597
+ ob_start();
598
+
599
+ ?>
600
+ <div class="<?php echo $type ?>">
601
+ <p><?php echo $message ?></p>
602
+ </div>
603
+ <?php
604
+ return ob_get_clean();
605
+ }
606
+
607
+ /**
608
+ * API Instance Singleton
609
+ * @return Object
610
+ */
611
+ public function api() {
612
+ // if ( is_null( self::$api_instance ) ) {
613
+ // if ( ! $this->has_api_key() ) {
614
+ // return false;
615
+ // }
616
+ // require_once( 'class-ss-wc-mailchimp-api.php' );
617
+ // self::$api_instance = new SS_WC_MailChimp_API( $this->api_key(), $this->debug_enabled() );
618
+ // }
619
+ // return self::$api_instance;
620
+ return ss_wc_mailchimp('api');
621
+ }
622
+
623
+ /**
624
+ * get_lists function.
625
+ *
626
+ * @access public
627
+ * @return void
628
+ */
629
+ public function get_lists() {
630
+
631
+ // $mailchimp_lists = get_transient( 'ss_wc_mailchimp_lists' );
632
+
633
+ // if ( ! $mailchimp_lists ) {
634
+ if ( $this->api() ) {
635
+ $mailchimp_lists = $this->api()->get_lists();
636
+ } else {
637
+ return false;
638
+ }
639
+
640
+ if ( $mailchimp_lists === false ) {
641
+
642
+ add_action( 'admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
643
+ add_action( 'network_admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
644
+
645
+ return false;
646
+
647
+ }
648
+
649
+ if ( count( $mailchimp_lists ) === 0 ) {
650
+ $default = array(
651
+ 'no_lists' => __( 'Oops! No lists in your MailChimp account...', $this->namespace ),
652
+ );
653
+ add_action( 'admin_notices', array( $this, 'mailchimp_no_lists_found' ) );
654
+ } else {
655
+ $default = array(
656
+ '' => __( 'Select a list...', $this->namespace ),
657
+ );
658
+ set_transient( $this->namespace_prefixed( 'lists' ), $mailchimp_lists, 60 * 60 * 1 );
659
+ }
660
+ $mailchimp_lists = array_merge( $default, $mailchimp_lists );
661
+
662
+ //}
663
+
664
+ return $mailchimp_lists;
665
+
666
+ }
667
+
668
+ /**
669
+ * get_interest_groups function.
670
+ *
671
+ * @access public
672
+ * @return void
673
+ */
674
+ public function get_interest_groups() {
675
+
676
+ if ( $this->api() && $this->has_list() ) {
677
+ $interest_groups = $this->api()->get_interest_categories_with_interests( $this->list() );
678
+ } else {
679
+ return false;
680
+ }
681
+
682
+ if ( $interest_groups === false ) {
683
+
684
+ add_action( 'admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
685
+ add_action( 'network_admin_notices', array( $this, 'mailchimp_api_error_msg' ) );
686
+
687
+ return false;
688
+
689
+ }
690
+
691
+ return $interest_groups;
692
+
693
+ }
694
+
695
+ /**
696
+ * Inform the user they don't have any MailChimp lists
697
+ */
698
+ public function mailchimp_no_lists_found() {
699
+ echo $this->get_message( sprintf( __( 'Oops! There are no lists in your MailChimp account. <a href="%s" target="_blank">Click here</a> to create one.', $this->namespace ), 'https://admin.mailchimp.com/lists/new-list/' ) );
700
+ }
701
+
702
+ /**
703
+ * Display message to user if there is an issue with the MailChimp API call
704
+ *
705
+ * @since 1.0
706
+ * @param void
707
+ * @return html the message for the user
708
+ */
709
+ public function mailchimp_api_error_msg() {
710
+ echo $this->get_message(
711
+ sprintf( __( 'Unable to load lists from MailChimp: (%s) %s. ', $this->namespace ), $this->api()->get_error_code(), $this->api()->get_error_message() ) .
712
+ sprintf( __( 'Please check your %s <a href="%s">settings</a>.', $this->namespace ), __( 'Settings', $this->namespace ), WOOCOMMERCE_MAILCHIMP_SETTINGS_URL )
713
+ );
714
+ } //end function mailchimp_api_error_msg
715
+
716
+ /**
717
+ * Helper log function for debugging
718
+ *
719
+ * @since 1.2.2
720
+ */
721
+ private function log( $message ) {
722
+ if ( $this->debug_enabled() ) {
723
+ $logger = new WC_Logger();
724
+
725
+ if ( is_array( $message ) || is_object( $message ) ) {
726
+ $logger->add( 'woocommerce-mailchimp', print_r( $message, true ) );
727
+ }
728
+ else {
729
+ $logger->add( 'woocommerce-mailchimp', $message );
730
+ }
731
+ }
732
+ }
733
+
734
+ public function sysinfo_field( $value ) {
735
+
736
+ // $option_value = self::get_option( $value['id'], $value['default'] );
737
+ $option_value = SS_System_Info::get_system_info();
738
+ // Description handling
739
+ $field_description = WC_Admin_Settings::get_field_description( $value );
740
+ extract( $field_description );
741
+ ?>
742
+ <tr valign="top">
743
+ <th scope="row" class="titledesc">
744
+ <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
745
+ <?php echo $tooltip_html; ?>
746
+ </th>
747
+ <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
748
+ <?php echo $description; ?>
749
+
750
+ <textarea
751
+ name="<?php echo esc_attr( $value['id'] ); ?>"
752
+ id="<?php echo esc_attr( $value['id'] ); ?>"
753
+ style="font-family: Menlo,Monaco,monospace;display: block; overflow: auto; white-space: pre; width: 800px; height: 400px;<?php echo esc_attr( $value['css'] ); ?>"
754
+ class="<?php echo esc_attr( $value['class'] ); ?>"
755
+ placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
756
+ readonly="readonly" onclick="this.focus(); this.select()"
757
+ <?php //echo implode( ' ', $custom_attributes ); ?>
758
+ ><?php echo esc_textarea( $option_value ); ?></textarea>
759
+ </td>
760
+ </tr>
761
+ <?php
762
+ }
763
+
764
+ } //end class SS_WC_MailChimp
765
+
766
+ return SS_WC_Settings_MailChimp::get_instance();
767
+
768
+ } //end if ( ! class_exists( 'SS_WC_Settings_MailChimp' ) )
includes/functions.php ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Get a service by its name
5
+ *
6
+ * _Example:_
7
+ *
8
+ * $forms = ss_wc_mailchimp('forms');
9
+ * $api = ss_wc_mailchimp('api');
10
+ *
11
+ * When no service parameter is given, the entire container will be returned.
12
+ *
13
+ * @ignore
14
+ * @access private
15
+ *
16
+ * @param string $service (optional)
17
+ * @return mixed
18
+ *
19
+ * @throws Exception when service is not found
20
+ */
21
+ function ss_wc_mailchimp( $service = null ) {
22
+ static $ss_wc_mailchimp;
23
+
24
+ if ( ! $ss_wc_mailchimp ) {
25
+ $ss_wc_mailchimp = new SS_WC_MailChimp_Container();
26
+ }
27
+
28
+ if ( $service ) {
29
+ return $ss_wc_mailchimp->get( $service );
30
+ }
31
+
32
+ return $ss_wc_mailchimp;
33
+ }
34
+
35
+ /**
36
+ * Gets the WooCommerce MailChimp options from the database
37
+ * Uses default values to prevent undefined index notices.
38
+ *
39
+ * @since 1.0
40
+ * @access public
41
+ * @static array $options
42
+ * @return array
43
+ */
44
+ function ss_wc_mailchimp_get_options() {
45
+ static $options;
46
+
47
+ if ( ! $options ) {
48
+ $defaults = require SS_WC_MAILCHIMP_DIR . 'config/default-settings.php';
49
+ $options = array();
50
+ foreach ( $defaults as $key => $val ) {
51
+ $options[ $key ] = get_option( 'ss_wc_mailchimp_' . $key );
52
+ }
53
+ $options = array_merge( $defaults, $options );
54
+ }
55
+
56
+ /**
57
+ * Filters the MailChimp for WordPress settings (general).
58
+ *
59
+ * @param array $options
60
+ */
61
+ return apply_filters( 'ss_wc_mailchimp_settings', $options );
62
+ }
63
+
64
+ /**
65
+ * Gets the WooCommerce MailChimp API Helper class and injects it with the API key
66
+ *
67
+ * @since 4.0
68
+ * @access public
69
+ *
70
+ * @return SS_WC_MailChimp
71
+ */
72
+ function ss_wc_mailchimp_get_api( $api_key = null, $debug = null ) {
73
+ $opts = ss_wc_mailchimp_get_options();
74
+ $instance = new SS_WC_MailChimp( ($api_key ? $api_key : $opts['api_key']), ($debug ? $debug : $opts['debug']) );
75
+ return $instance;
76
+ }
includes/lib/class-ss-system-info.php ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
+
5
+ if ( ! class_exists( 'SS_System_Info' ) ) {
6
+
7
+ /**
8
+ * Helper class for generating system information on WordPress installations
9
+ */
10
+ final class SS_System_Info {
11
+
12
+ /**
13
+ * Get system info
14
+ *
15
+ * @since 2.0
16
+ * @access public
17
+ * @global object $wpdb Used to query the database using the WordPress Database API
18
+ * @return string $return A string containing the info to output
19
+ */
20
+ public static function get_system_info() {
21
+ global $wpdb;
22
+
23
+ // Get theme info
24
+ $theme_data = wp_get_theme();
25
+ $theme = $theme_data->Name . ' ' . $theme_data->Version;
26
+
27
+ // Try to identify the hosting provider
28
+ $host = self::get_host();
29
+
30
+ $return = '### Begin System Info ###' . "\n\n";
31
+
32
+ // Start with the basics...
33
+ $return .= '-- Site Info' . "\n\n";
34
+ $return .= 'Site URL: ' . site_url() . "\n";
35
+ $return .= 'Home URL: ' . home_url() . "\n";
36
+ $return .= 'Multisite: ' . ( is_multisite() ? 'Yes' : 'No' ) . "\n";
37
+
38
+ // Can we determine the site's host?
39
+ if( $host ) {
40
+ $return .= "\n" . '-- Hosting Provider' . "\n\n";
41
+ $return .= 'Host: ' . $host . "\n";
42
+ }
43
+
44
+ // WordPress configuration
45
+ $return .= "\n" . '-- WordPress Configuration' . "\n\n";
46
+ $return .= 'Version: ' . get_bloginfo( 'version' ) . "\n";
47
+ $return .= 'Language: ' . ( defined( 'WPLANG' ) && WPLANG ? WPLANG : 'en_US' ) . "\n";
48
+ $return .= 'Permalink Structure: ' . ( get_option( 'permalink_structure' ) ? get_option( 'permalink_structure' ) : 'Default' ) . "\n";
49
+ $return .= 'Active Theme: ' . $theme . "\n";
50
+ $return .= 'Show On Front: ' . get_option( 'show_on_front' ) . "\n";
51
+
52
+ // Only show page specs if frontpage is set to 'page'
53
+ if( get_option( 'show_on_front' ) == 'page' ) {
54
+ $front_page_id = get_option( 'page_on_front' );
55
+ $blog_page_id = get_option( 'page_for_posts' );
56
+
57
+ $return .= 'Page On Front: ' . ( $front_page_id != 0 ? get_the_title( $front_page_id ) . ' (#' . $front_page_id . ')' : 'Unset' ) . "\n";
58
+ $return .= 'Page For Posts: ' . ( $blog_page_id != 0 ? get_the_title( $blog_page_id ) . ' (#' . $blog_page_id . ')' : 'Unset' ) . "\n";
59
+ }
60
+
61
+ $return .= 'ABSPATH: ' . ABSPATH . "\n";
62
+
63
+ // Make sure wp_remote_post() is working
64
+ $request['cmd'] = '_notify-validate';
65
+
66
+ $params = array(
67
+ 'sslverify' => false,
68
+ 'timeout' => 60,
69
+ 'user-agent' => 'SS_System_Info/',
70
+ 'body' => $request
71
+ );
72
+
73
+ $response = wp_remote_post( 'https://www.paypal.com/cgi-bin/webscr', $params );
74
+
75
+ if( !is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
76
+ $WP_REMOTE_POST = 'wp_remote_post() works';
77
+ } else {
78
+ $WP_REMOTE_POST = 'wp_remote_post() does not work';
79
+ }
80
+
81
+ $return .= 'Remote Post: ' . $WP_REMOTE_POST . "\n";
82
+ $return .= 'Table Prefix: ' . 'Length: ' . strlen( $wpdb->prefix ) . ' Status: ' . ( strlen( $wpdb->prefix ) > 16 ? 'ERROR: Too long' : 'Acceptable' ) . "\n";
83
+
84
+ $return .= 'WP_DEBUG: ' . ( defined( 'WP_DEBUG' ) ? WP_DEBUG ? 'Enabled' : 'Disabled' : 'Not set' ) . "\n";
85
+ $return .= 'Memory Limit: ' . WP_MEMORY_LIMIT . "\n";
86
+ $return .= 'Registered Post Stati: ' . implode( ', ', get_post_stati() ) . "\n";
87
+
88
+ // Get plugins that have an update
89
+ $updates = get_plugin_updates();
90
+
91
+ // Must-use plugins
92
+ // NOTE: MU plugins can't show updates!
93
+ $muplugins = get_mu_plugins();
94
+ if( count( $muplugins ) > 0 ) {
95
+ $return .= "\n" . '-- Must-Use Plugins' . "\n\n";
96
+
97
+ foreach( $muplugins as $plugin => $plugin_data ) {
98
+ $return .= $plugin_data['Name'] . ': ' . $plugin_data['Version'] . "\n";
99
+ }
100
+
101
+ $return = apply_filters( 'edd_sysinfo_after_wordpress_mu_plugins', $return );
102
+ }
103
+
104
+ // WordPress active plugins
105
+ $return .= "\n" . '-- WordPress Active Plugins' . "\n\n";
106
+
107
+ $plugins = get_plugins();
108
+ $active_plugins = get_option( 'active_plugins', array() );
109
+
110
+ foreach( $plugins as $plugin_path => $plugin ) {
111
+ if( !in_array( $plugin_path, $active_plugins ) )
112
+ continue;
113
+
114
+ $update = ( array_key_exists( $plugin_path, $updates ) ) ? ' (needs update - ' . $updates[$plugin_path]->update->new_version . ')' : '';
115
+ $return .= $plugin['Name'] . ': ' . $plugin['Version'] . $update . "\n";
116
+ }
117
+
118
+ // WordPress inactive plugins
119
+ $return .= "\n" . '-- WordPress Inactive Plugins' . "\n\n";
120
+
121
+ foreach( $plugins as $plugin_path => $plugin ) {
122
+ if( in_array( $plugin_path, $active_plugins ) )
123
+ continue;
124
+
125
+ $update = ( array_key_exists( $plugin_path, $updates ) ) ? ' (needs update - ' . $updates[$plugin_path]->update->new_version . ')' : '';
126
+ $return .= $plugin['Name'] . ': ' . $plugin['Version'] . $update . "\n";
127
+ }
128
+
129
+ if( is_multisite() ) {
130
+ // WordPress Multisite active plugins
131
+ $return .= "\n" . '-- Network Active Plugins' . "\n\n";
132
+
133
+ $plugins = wp_get_active_network_plugins();
134
+ $active_plugins = get_site_option( 'active_sitewide_plugins', array() );
135
+
136
+ foreach( $plugins as $plugin_path ) {
137
+ $plugin_base = plugin_basename( $plugin_path );
138
+
139
+ if( !array_key_exists( $plugin_base, $active_plugins ) )
140
+ continue;
141
+
142
+ $update = ( array_key_exists( $plugin_path, $updates ) ) ? ' (needs update - ' . $updates[$plugin_path]->update->new_version . ')' : '';
143
+ $plugin = get_plugin_data( $plugin_path );
144
+ $return .= $plugin['Name'] . ': ' . $plugin['Version'] . $update . "\n";
145
+ }
146
+
147
+ }
148
+
149
+ // Server configuration (really just versioning)
150
+ $return .= "\n" . '-- Webserver Configuration' . "\n\n";
151
+ $return .= 'PHP Version: ' . PHP_VERSION . "\n";
152
+ $return .= 'MySQL Version: ' . $wpdb->db_version() . "\n";
153
+ $return .= 'Webserver Info: ' . $_SERVER['SERVER_SOFTWARE'] . "\n";
154
+
155
+ // PHP configs... now we're getting to the important stuff
156
+ $return .= "\n" . '-- PHP Configuration' . "\n\n";
157
+ $return .= 'Safe Mode: ' . ( ini_get( 'safe_mode' ) ? 'Enabled' : 'Disabled' . "\n" );
158
+ $return .= 'Memory Limit: ' . ini_get( 'memory_limit' ) . "\n";
159
+ $return .= 'Upload Max Size: ' . ini_get( 'upload_max_filesize' ) . "\n";
160
+ $return .= 'Post Max Size: ' . ini_get( 'post_max_size' ) . "\n";
161
+ $return .= 'Upload Max Filesize: ' . ini_get( 'upload_max_filesize' ) . "\n";
162
+ $return .= 'Time Limit: ' . ini_get( 'max_execution_time' ) . "\n";
163
+ $return .= 'Max Input Vars: ' . ini_get( 'max_input_vars' ) . "\n";
164
+ $return .= 'Display Errors: ' . ( ini_get( 'display_errors' ) ? 'On (' . ini_get( 'display_errors' ) . ')' : 'N/A' ) . "\n";
165
+ $return .= 'PHP Arg Separator: ' . ini_get( 'arg_separator.output' ) . "\n";
166
+
167
+ // PHP extensions and such
168
+ $return .= "\n" . '-- PHP Extensions' . "\n\n";
169
+ $return .= 'cURL: ' . ( function_exists( 'curl_init' ) ? 'Supported' : 'Not Supported' ) . "\n";
170
+ $return .= 'fsockopen: ' . ( function_exists( 'fsockopen' ) ? 'Supported' : 'Not Supported' ) . "\n";
171
+ $return .= 'SOAP Client: ' . ( class_exists( 'SoapClient' ) ? 'Installed' : 'Not Installed' ) . "\n";
172
+ $return .= 'Suhosin: ' . ( extension_loaded( 'suhosin' ) ? 'Installed' : 'Not Installed' ) . "\n";
173
+
174
+ $return = apply_filters( 'edd_sysinfo_after_php_ext', $return );
175
+
176
+ // Session stuff
177
+ $return .= "\n" . '-- Session Configuration' . "\n\n";
178
+ $return .= 'Session: ' . ( isset( $_SESSION ) ? 'Enabled' : 'Disabled' ) . "\n";
179
+
180
+ // The rest of this is only relevant is session is enabled
181
+ if( isset( $_SESSION ) ) {
182
+ $return .= 'Session Name: ' . esc_html( ini_get( 'session.name' ) ) . "\n";
183
+ $return .= 'Cookie Path: ' . esc_html( ini_get( 'session.cookie_path' ) ) . "\n";
184
+ $return .= 'Save Path: ' . esc_html( ini_get( 'session.save_path' ) ) . "\n";
185
+ $return .= 'Use Cookies: ' . ( ini_get( 'session.use_cookies' ) ? 'On' : 'Off' ) . "\n";
186
+ $return .= 'Use Only Cookies: ' . ( ini_get( 'session.use_only_cookies' ) ? 'On' : 'Off' ) . "\n";
187
+ }
188
+
189
+ $return .= "\n" . '### End System Info ###';
190
+
191
+ return $return;
192
+
193
+ } //end function sysinfo_get
194
+
195
+ /**
196
+ * Get user host
197
+ *
198
+ * Returns the webhost this site is using if possible
199
+ *
200
+ * @since 2.0
201
+ * @return mixed string $host if detected, false otherwise
202
+ */
203
+ public static function get_host() {
204
+ $host = false;
205
+
206
+ if( defined( 'WPE_APIKEY' ) ) {
207
+ $host = 'WP Engine';
208
+ } elseif( defined( 'PAGELYBIN' ) ) {
209
+ $host = 'Pagely';
210
+ } elseif( DB_HOST == 'localhost:/tmp/mysql5.sock' ) {
211
+ $host = 'ICDSoft';
212
+ } elseif( DB_HOST == 'mysqlv5' ) {
213
+ $host = 'NetworkSolutions';
214
+ } elseif( strpos( DB_HOST, 'ipagemysql.com' ) !== false ) {
215
+ $host = 'iPage';
216
+ } elseif( strpos( DB_HOST, 'ipowermysql.com' ) !== false ) {
217
+ $host = 'IPower';
218
+ } elseif( strpos( DB_HOST, '.gridserver.com' ) !== false ) {
219
+ $host = 'MediaTemple Grid';
220
+ } elseif( strpos( DB_HOST, '.pair.com' ) !== false ) {
221
+ $host = 'pair Networks';
222
+ } elseif( strpos( DB_HOST, '.stabletransit.com' ) !== false ) {
223
+ $host = 'Rackspace Cloud';
224
+ } elseif( strpos( DB_HOST, '.sysfix.eu' ) !== false ) {
225
+ $host = 'SysFix.eu Power Hosting';
226
+ } elseif( strpos( $_SERVER['SERVER_NAME'], 'Flywheel' ) !== false ) {
227
+ $host = 'Flywheel';
228
+ } else {
229
+ // Adding a general fallback for data gathering
230
+ $host = 'DBH: ' . DB_HOST . ', SRV: ' . $_SERVER['SERVER_NAME'];
231
+ }
232
+
233
+ return $host;
234
+
235
+ } //end function get_host
236
+
237
+ }
238
+
239
+ }
includes/migrations/class-ss-wc-migration-from-1.3.X-to-2.0.php ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ final class SS_WC_MailChimp_Migration_From_13X_To_20 extends SS_WC_MailChimp_Migration_Base {
4
+
5
+ public function __construct( $current_version, $target_version ) {
6
+ parent::__construct( $current_version, $target_version );
7
+ }
8
+
9
+ /**
10
+ * [up description]
11
+ * @return [type] [description]
12
+ */
13
+ public function up() {
14
+
15
+ $interest_groupings_key = 'interest_groupings';
16
+ $groups_key = 'groups';
17
+
18
+ try {
19
+
20
+ if ( !array_key_exists( $interest_groupings_key, $this->settings ) || !array_key_exists( $groups_key, $this->settings ) ) return;
21
+
22
+ $list = $this->settings['list'];
23
+ $interest_groupings = $this->settings[$interest_groupings_key];
24
+ $groups = $this->settings[$groups_key];
25
+
26
+ if ( empty( $interest_groupings ) || empty( $groups ) || empty( $list ) ) return;
27
+
28
+ $interest_categories = $this->api->get_interest_categories( $list );
29
+
30
+ $selected_interest_category = array_filter( $interest_categories, function($v) use($interest_groupings) {
31
+ return $v == $interest_groupings;
32
+ } );
33
+
34
+ $selected_interest_category_id = key( $selected_interest_category );
35
+
36
+ $interest_category_interests = $this->api->get_interest_category_interests( $list, $selected_interest_category_id );
37
+
38
+ $groups = explode( ',', $groups );
39
+
40
+ $selected_interest_category_interests = array_intersect( $interest_category_interests, $groups );
41
+
42
+ $interest_groups = array_keys($selected_interest_category_interests);
43
+
44
+ $this->settings['interest_groups'] = $interest_groups;
45
+
46
+ unset($this->settings[$interest_groupings_key]);
47
+ unset($this->settings[$groups_key]);
48
+
49
+ $this->save_settings();
50
+ } catch (Exception $e) {
51
+ // If all else fails, finish the upgrade and the user will have to reselect the interest groups.
52
+ unset($this->settings[$interest_groupings_key]);
53
+ unset($this->settings[$groups_key]);
54
+
55
+ $this->save_settings();
56
+ }
57
+
58
+ return true;
59
+
60
+ }
61
+
62
+ /**
63
+ * [down description]
64
+ * @return [type] [description]
65
+ */
66
+ public function down() {
67
+
68
+ throw new Exception('Cannot rollback to versions prior to 2.0');
69
+
70
+ }
71
+
72
+ }
includes/migrations/class-ss-wc-migration.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ abstract class SS_WC_MailChimp_Migration_Base {
4
+
5
+ protected $api;
6
+
7
+ protected $settings;
8
+
9
+ protected $namespace = 'ss_wc_mailchimp';
10
+
11
+ protected $current_version;
12
+ protected $target_version;
13
+
14
+ protected $setting_keys = array(
15
+ 'api_key',
16
+ 'enabled',
17
+ 'list',
18
+ 'interest_groups',
19
+ 'display_opt_in',
20
+ 'occurs',
21
+ 'double_opt_in',
22
+ 'opt_in_label',
23
+ 'opt_in_checkbox_default_status',
24
+ 'opt_in_checkbox_display_location',
25
+ );
26
+
27
+ protected $settings_key = 'woocommerce_mailchimp_settings';
28
+
29
+ public function __construct( $current_version, $target_version ) {
30
+
31
+ $this->settings = array();
32
+
33
+ $this->current_version = $current_version;
34
+ $this->target_version = $target_version;
35
+
36
+ if ( ! $this->load_settings() ) return;
37
+
38
+ if ( empty( $this->settings['api_key'] ) ) return;
39
+
40
+ require_once( SS_WC_MAILCHIMP_DIR . 'includes/class-ss-wc-mailchimp.php' );
41
+ $this->api = new SS_WC_MailChimp( $this->settings['api_key'] );
42
+ }
43
+
44
+ /**
45
+ * [save_settings description]
46
+ * @return [type] [description]
47
+ */
48
+ public function load_settings() {
49
+
50
+ if ( $this->current_version === '1.3.X' ) {
51
+
52
+ if ( ! $this->settings = get_option( $this->settings_key ) ) {
53
+ return false;
54
+ }
55
+
56
+ if ( !is_array( $this->settings ) ) {
57
+ return false;
58
+ }
59
+
60
+ } else {
61
+
62
+ foreach ( $this->$setting_keys as $key ) {
63
+ $this->settings[ $key ] = get_option( $this->namespace_prefixed( $key ) );
64
+ }
65
+
66
+ }
67
+
68
+ return true;
69
+
70
+ }
71
+
72
+ /**
73
+ * [save_settings description]
74
+ * @return [type] [description]
75
+ */
76
+ public function save_settings() {
77
+
78
+ if ( $this->target_version === '1.3.X' ) {
79
+
80
+ update_option( $this->settings_key , $this->settings );
81
+
82
+ } else {
83
+
84
+ foreach ( $this->settings as $key => $value ) {
85
+ update_option( $this->namespace_prefixed( $key ), $value );
86
+ }
87
+
88
+ }
89
+
90
+ }
91
+
92
+ private function namespace_prefixed( $suffix ) {
93
+ return $this->namespace . '_' . $suffix;
94
+ }
95
+
96
+ }
index.php ADDED
@@ -0,0 +1 @@
 
1
+ <?php // Silence is golden
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === WooCommerce MailChimp ===
2
- Contributors: anderly, saintsystems
3
  Tags: woocommerce, mailchimp
4
  Requires at least: 3.5.1
5
  Tested up to: 4.6.1
6
- Stable tag: 1.3.9
7
  License: GPLv3
8
 
9
  Simple and flexible MailChimp integration for WooCommerce.
@@ -19,6 +19,7 @@ Automatically subscribe customers to a designated MailChimp list and, optionally
19
  **WooCommerce Event Selection**
20
 
21
  - Subscribe customers to MailChimp after order creation
 
22
  - Subscribe customers to MailChimp after order completion
23
 
24
  **Works with MailChimp Interest Groups**
@@ -55,7 +56,12 @@ WooCommerce MailChimp requires PHP 5.4+ (PHP 7.0+ recommended). You'll also need
55
 
56
  = Documentation & Support =
57
 
58
- Online documentation and code samples are available via our [Help Center](https://support.saintsystems.com/hc/en-us/sections/201959566). Given the free nature of this plugin, we cannot offer dedicated support via the WordPress.org forums (although we do check them). If you are interested in purchasing dedicated please contact us via our help center to get quote.
 
 
 
 
 
59
 
60
  = Contribute =
61
  All development for WooCommerce MailChimp is [handled via GitHub](https://github.com/anderly/woocommerce-mailchimp). Opening new issues and submitting pull requests are welcome.
@@ -68,110 +74,218 @@ Also, if you enjoy using the software [we'd love it if you could give us a revie
68
 
69
  == Installation ==
70
 
71
- 1. Upload or extract the `woocommerce-mailchimp` folder to your site's `/wp-content/plugins/` directory. You can also use the *Add new* option found in the *Plugins* menu in WordPress.
72
- 2. Enable the plugin from the *Plugins* menu in WordPress.
73
 
74
  = Usage =
75
 
76
- 1. Go to WooCommerce > Settings > Integration > MailChimp
77
- 2. First, enable the plugin and set your MailChimp API Key and hit save.
78
- 3. Select whether you want customers to be subscribed to your MailChimp list after order creation or order completion (there's a difference in WooCommerce).
79
- 4. Next, select your MailChimp list and set any interest group settings (optional) and hit save.
80
- 5. That's it, now customers who purchase products from your WooCommerce store will automatically be subscribed to the selected list (and optional interest groups) in MailChimp!
 
81
 
82
  == Screenshots ==
83
 
84
- 1. WooCommerce MailChimp options screen.
 
85
 
86
  == Changelog ==
87
 
88
- = 1.3.9 =
89
- * Rename MCAPI class to prevent collisions with other plugins using MCAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- = 1.3.8 =
92
- * Tested up to WordPress 4.6.1
93
- * Tested up to WooCommerce 2.6.4
94
- * More flexible opt_in checkbox placement
95
- * Pass $order_id to `ss_wc_mailchimp_subscribe_options` hook
96
- * Use only one instance of MCAPI
97
- * Fixed Issue #14 MCAPI constructor style
98
- * Fixed Issue #15 `mailchimp_api_error_msg`
99
- * Fixed Issue #16 where lists wouldn't show up until you saved the settings twice
100
 
101
- = 1.3.7 =
102
- * WordPress 4.4 Compatible
103
- * WooCommerce 2.4.12 Compatible
104
- * API response not shown in debug log
105
- * Use only one instance of MCAPI
106
 
107
- = 1.3.6 =
108
- * Backout of change to use WC_Logger due to fatal error
 
 
109
 
110
- = 1.3.5 =
111
- * Fix for undefined variable list and array_merge issue.
112
- * Change to use WC_Logger instead of error_log
113
- * Updated pot file
114
- * Added French translation
115
- * General code syntax cleanup
116
 
117
- = 1.3.4 =
118
- * Fix enabled check. Issue #6.
119
 
120
- = 1.3.3 =
121
- * Fix for transient key length. Tested with WordPress 4.1
122
 
123
- = 1.3.2 =
124
- * Fix for headers already sent message. Tested with WordPress 4.0 and WooCommerce 2.2.*
125
 
126
- = 1.3.1 =
127
- * Fix for MailChimp merge vars bug introduced in v1.3
128
 
129
- = 1.3 =
130
- * Breaking Change: 'ss_wc_mailchimp_subscribe_merge_vars' action filter now passes $order_id param to enable retrieving additional order info/meta to use in MailChimp merge vars
131
- * Small fix to order_created subscribe event to work with PayPal Payment Gateway
132
 
133
- = 1.2.6 =
134
- * Added additional debug logging when WP_DEBUG is enabled
135
 
136
- = 1.2.5 =
137
- * Bug fix for subscribe when not using opt-in display field
138
 
139
- = 1.2.4 =
140
- * Bug fix for subscribe
141
 
142
- = 1.2.3 =
143
- * Fixed plugin settings link for WooCommerce 2.1
144
 
145
- = 1.2.2 =
146
- * WooCommerce 2.1 fix for order custom fields
147
 
148
- = 1.2.1 =
149
- * WooCommerce 2.1 integration: Change to use wc_enqueue_js instead of add_inline_js
150
- * WooCommerce 2.1 integration: Change to support new default checkout field filter for default opt-in checkbox status
151
 
152
- = 1.2 =
153
- * Added new setting to control whether or not the double opt-in checkbox is checked/unchecked by default on the checkout page.
154
- * Added new setting to control display location of the double opt-in checkbox (under billing info or order info)
155
- * Small modification to append to MailChimp interest groups for existing users so that group settings are not lost for users who were already subscribed.
156
- * Preparations for i18n (Internationalization) support. Several users have already asked and offered to translate the plugin into other languages. We had always planned on that, but now are making that a reality.
157
 
158
- = 1.1.3 =
159
- * Minor action hook change since order meta (needed for MailChimp API call) is not yet available on 'woocommerce_new_order' hook
160
 
161
- = 1.1.2 =
162
- * Update to REALLY address issue with subscriptions not occurring on order create "pending"
163
 
164
- = 1.1.1 =
165
- * Update to address issue with subscriptions not occurring on order create "pending"
166
 
167
- = 1.1 =
168
- * Add the option to display an opt-in field on checkout
169
 
170
- = 1.0.2 =
171
- * Minor text and comment changes
172
 
173
- = 1.0.1 =
174
- * Added "Settings" link on the Plugins administration screen
175
 
176
- = 1.0 =
177
- * This is the first public release.
1
  === WooCommerce MailChimp ===
2
+ Contributors: saintsystems, anderly
3
  Tags: woocommerce, mailchimp
4
  Requires at least: 3.5.1
5
  Tested up to: 4.6.1
6
+ Stable tag: 2.0
7
  License: GPLv3
8
 
9
  Simple and flexible MailChimp integration for WooCommerce.
19
  **WooCommerce Event Selection**
20
 
21
  - Subscribe customers to MailChimp after order creation
22
+ - Subscribe customers to MailChimp after order processing
23
  - Subscribe customers to MailChimp after order completion
24
 
25
  **Works with MailChimp Interest Groups**
56
 
57
  = Documentation & Support =
58
 
59
+ Online documentation and code samples are available via our [Help Center](https://support.saintsystems.com/hc/en-us/sections/201959566).
60
+
61
+ Please visit the
62
+ [WooCommerce MailChimp support forum on WordPress.org](https://wordpress.org/support/plugin/woocommerce-mailchimp) for basic support and help from other users. Since this is a free plugin, we respond to these as we have time.
63
+
64
+ Dedicated support will be available with the upcoming [WooCommerce MailChimp Pro](https://www.saintsystems.com/products/woocommerce-mailchimp-pro/).
65
 
66
  = Contribute =
67
  All development for WooCommerce MailChimp is [handled via GitHub](https://github.com/anderly/woocommerce-mailchimp). Opening new issues and submitting pull requests are welcome.
74
 
75
  == Installation ==
76
 
77
+ 1. Upload or extract the `woocommerce-mailchimp` folder to your site's `/wp-content/plugins/` directory. You can also use the *Add new- option found in the *Plugins- menu in WordPress.
78
+ 2. Enable the plugin from the *Plugins- menu in WordPress.
79
 
80
  = Usage =
81
 
82
+ 1. Go to WooCommerce > Settings > MailChimp
83
+ 2. First, paste your MailChimp API Key to get started.
84
+ 3. Select whether you want customers to be subscribed to your MailChimp list after order creation, order processing or order completion (there's a difference in WooCommerce).
85
+ 4. Next, select your MailChimp list and select any interest groups (optional).
86
+ 5. Select your opt-in, double opt-in settings and hit `Save changes`.
87
+ 6. That's it, your WooCommerce store is now integrated with MailChimp!
88
 
89
  == Screenshots ==
90
 
91
+ 1. WooCommerce MailChimp general settings screen.
92
+ 2. WooCommerce MailChimp troubleshooting screen.
93
 
94
  == Changelog ==
95
 
96
+ #### 2.0 - September 16, 2016
97
+
98
+ **WARNING:** This release contains breaking changes to the plugins action hooks and filters. If you have custom code that hooks into the plugins action hooks and filters, please review the breaking changes below to know how to update your code appropriately.
99
+
100
+ **Breaking Changes**
101
+
102
+ - Action filter `ss_wc_mailchimp_subscribe_merge_vars` is now `ss_wc_mailchimp_subscribe_merge_tags`
103
+ - The filter no longer contains the `GROUPINGS` sub-key for [MailChimp Groups](http://kb.mailchimp.com/groups) due to a change with the MailChimp API v3.0.
104
+ - The filter now only contains the [MailChimp Merge Tags](http://kb.mailchimp.com/merge-tags).
105
+ - [MailChimp Groups](http://kb.mailchimp.com/groups) now have their own action filter `ss_wc_mailchimp_subscribe_interest_groups`
106
+ - Action filter `ss_wc_mailchimp_subscribe_options` has changed due to changes with the MailChimp API v3.0.
107
+ - The key `listid` has been changed to `list_id` in the `$subscribe_options` parameter
108
+ - The `vars` key has been removed from the `$subscribe_options` parameter (this key previously contained the merge tags and groups together).
109
+ - The `update_existing`, `replace_interests` and `send_welcome` keys have been removed from the `$subscribe_options` parameter since they are no longer supported by the API.
110
+ - The plugin now always updates existing subscribers if they exist.
111
+ - The plugin now always appends interest groups and does not affect existing groups on subscribers.
112
+ - The send welcome email can be configured on the target list and is not required to be sent through the API.
113
+ - A new key `merge_tags` has been added and includes the `$merge_tags` array to be sent to the MailChimp API
114
+ - A new key `interest_groups` has been added and includes the `$interest_groups` array to be sent to the MailChimp API
115
+
116
+
117
+ **Improvements**
118
+
119
+ - Added ability to pick MailChimp Interest Groups from drop-down list.
120
+ - Upgraded MailChimp API to v3.0
121
+
122
+ **Additions**
123
+
124
+ - Added `Debug Log` setting to enable/disable logging for troubleshooting purposes.
125
+ - Added `System Info` setting for troubleshooting purposes.
126
+ - New action hook `ss_wc_mailchimp_loaded` fired after the plugin has loaded.
127
+ - New action hook `ss_wc_mailchimp_before_opt_in_checkbox` fired before opt in checkbox is rendered.
128
+ - New action filter `ss_wc_mailchimp_opt_in_checkbox` allows for overriding opt in checkbox rendering
129
+ - New action hook `ss_wc_mailchimp_after_opt_in_checkbox` fired after opt in checkbox is rendered.
130
+
131
+ #### 1.3.9 - September 13, 2016
132
+
133
+ **Fixes**
134
+
135
+ - Rename MCAPI class to prevent collisions with other plugins.
136
+
137
+ #### 1.3.8 - September 9, 2016
138
+
139
+ **Improvements**
140
+
141
+ - Tested up to WordPress 4.6.1
142
+ - Tested up to WooCommerce 2.6.4
143
+ - More flexible opt_in checkbox placement
144
+ - Pass $order_id to `ss_wc_mailchimp_subscribe_options` hook
145
+
146
+ **Fixes**
147
+
148
+ - Use only one instance of MCAPI
149
+ - Fixed Issue #14 MCAPI constructor style
150
+ - Fixed Issue #15 `mailchimp_api_error_msg`
151
+ - Fixed Issue #16 where lists wouldn't show up until you saved the settings twice
152
+
153
+ #### 1.3.7 - December 16, 2015
154
+
155
+ **Improvements**
156
+
157
+ - WordPress 4.4 Compatible
158
+ - WooCommerce 2.4.12 Compatible
159
+
160
+ **Fixes**
161
+
162
+ - API response not shown in debug log
163
+ - Use only one instance of MCAPI
164
+
165
+ #### 1.3.6 - February 9, 2015
166
+
167
+ **Fixes**
168
+
169
+ - Backout of change to use WC_Logger due to fatal error
170
+
171
+ #### 1.3.5 - February 6, 2015
172
+
173
+ **Improvements**
174
+
175
+ - Change to use WC_Logger instead of error_log
176
+ - Updated pot file
177
+ - Added French translation
178
+ - General code syntax cleanup
179
+
180
+ **Fixes**
181
+
182
+ - Fix for undefined variable list and array_merge issue.
183
+
184
+ #### 1.3.3 & 1.3.4 - January 16, 2016
185
+
186
+ **Fixes**
187
+
188
+ - Fix enabled check. Issue #6.
189
+ - Fix for transient key length.
190
+
191
+ **Improvements**
192
+
193
+ - Tested with WordPress 4.1
194
+
195
+ #### 1.3.2 - November 4, 2014
196
+
197
+ **Fixes*
198
+
199
+ - Fix for headers already sent message. Tested with WordPress 4.0 and WooCommerce 2.2.*
200
+
201
+ #### 1.3.1 - April 3, 2014
202
+
203
+ **Fixes**
204
+
205
+ - Fix for MailChimp merge vars bug introduced in v1.3
206
+
207
+ #### 1.3 - April 1, 2014
208
+
209
+ **Breaking Changes**
210
+
211
+ - Action filer `ss_wc_mailchimp_subscribe_merge_vars` now passes $order_id param to enable retrieving additional order info/meta to use in MailChimp merge vars
212
+
213
+ **Fixes**
214
+
215
+ - Small fix to order_created subscribe event to work with PayPal Payment Gateway
216
+
217
+ #### 1.2.6 - February 28, 2014
218
+
219
+ **Improvements**
220
+
221
+ - Added additional debug logging when WP_DEBUG is enabled
222
+
223
+ #### 1.2.5 - February 27, 2014
224
+
225
+ **Fixes**
226
+
227
+ - Bug fix for subscribe when not using opt-in display field
228
+
229
+ #### 1.2.2, 1.2.3 & 1.2.4 - February 22, 2014
230
+
231
+ **Fixes**
232
+
233
+ - WooCommerce 2.1 fix for order custom fields
234
+ - Fixed plugin settings link for WooCommerce 2.1
235
+ - Bug fix for subscribe
236
+
237
+ #### 1.2.1 - February 13, 2014
238
+
239
+ **Improvements**
240
+
241
+ - WooCommerce 2.1 integration: Change to use wc_enqueue_js instead of add_inline_js
242
+ - WooCommerce 2.1 integration: Change to support new default checkout field filter for default opt-in checkbox status
243
 
244
+ #### 1.2 - January 10, 2014
 
 
 
 
 
 
 
 
245
 
246
+ **Improvements**
 
 
 
 
247
 
248
+ - Added new setting to control whether or not the double opt-in checkbox is checked/unchecked by default on the checkout page.
249
+ - Added new setting to control display location of the double opt-in checkbox (under billing info or order info)
250
+ - Small modification to append to MailChimp interest groups for existing users so that group settings are not lost for users who were already subscribed.
251
+ - Preparations for i18n (Internationalization) support. Several users have already asked and offered to translate the plugin into other languages. We had always planned on that, but now are making that a reality.
252
 
253
+ ### 1.1.2 & 1.1.3 - January 2, 2014
 
 
 
 
 
254
 
255
+ **Fixes**
 
256
 
257
+ - Update to REALLY address issue with subscriptions not occurring on order create "pending"
 
258
 
259
+ **Additions**
 
260
 
261
+ - Minor action hook change since order meta (needed for MailChimp API call) is not yet available on 'woocommerce_new_order' hook
 
262
 
263
+ ### - January 2, 2014
 
 
264
 
265
+ ### 1.1.1 - December 31, 2013
 
266
 
267
+ **Fixes**
 
268
 
269
+ - Update to address issue with subscriptions not occurring on order create "pending"
 
270
 
271
+ ### 1.1 - November 12, 2013
 
272
 
273
+ **Improvements**
 
274
 
275
+ - Add the option to display an opt-in field on checkout
 
 
276
 
277
+ ### 1.0.2 - October 16, 2013
 
 
 
 
278
 
279
+ **Additions**
 
280
 
281
+ - Minor text and comment changes
 
282
 
283
+ ### 1.0.1 - October 11, 2013
 
284
 
285
+ **Improvements**
 
286
 
287
+ - Added "Settings" link on the Plugins administration screen
 
288
 
289
+ ### 1.0 - October 10, 2013
 
290
 
291
+ - This is the first public release.
 
screenshot-1.png DELETED
Binary file
woocommerce-mailchimp.php CHANGED
@@ -1,69 +1,54 @@
1
  <?php
2
  /**
3
  * Plugin Name: WooCommerce MailChimp
4
- * Plugin URI: http://anderly.com/woocommerce-mailchimp
5
  * Description: WooCommerce MailChimp provides simple MailChimp integration for WooCommerce.
6
- * Author: Adam Anderly
7
- * Author URI: http://anderly.com
8
- * Version: 1.3.9
9
  * Text Domain: ss_wc_mailchimp
10
  * Domain Path: languages
11
  *
12
- * Copyright: � 2015 Adam Anderly
13
  * License: GNU General Public License v3.0
14
  * License URI: http://www.gnu.org/licenses/gpl-3.0.html
15
- *
16
- * MailChimp Docs: http://apidocs.mailchimp.com/
17
  */
18
 
 
 
 
 
19
 
20
- function woocommerce_mailchimp_init() {
21
-
22
- if ( ! class_exists( 'WC_Integration' ) )
23
- return;
24
-
25
- load_plugin_textdomain( 'ss_wc_mailchimp', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
26
-
27
- global $woocommerce;
28
-
29
- $settings_url = admin_url( 'admin.php?page=woocommerce_settings&tab=integration&section=mailchimp' );
30
-
31
- if ( $woocommerce->version >= '2.1' ) {
32
- $settings_url = admin_url( 'admin.php?page=wc-settings&tab=integration&section=mailchimp' );
33
- }
34
-
35
- if ( ! defined( 'WOOCOMMERCE_MAILCHIMP_SETTINGS_URL' ) ) {
36
- define( 'WOOCOMMERCE_MAILCHIMP_SETTINGS_URL', $settings_url );
37
- }
38
-
39
- include_once( 'classes/class-ss-wc-integration-mailchimp.php' );
40
 
41
- /**
42
- * Add the Integration to WooCommerce
43
- */
44
- function add_mailchimp_integration( $integrations ) {
45
- $integrations[] = 'SS_WC_Integration_MailChimp';
46
 
47
- return $integrations;
48
- }
 
 
49
 
50
- add_filter( 'woocommerce_integrations', 'add_mailchimp_integration' );
 
 
 
 
51
 
52
- /**
53
- * Add Settings link to plugins list
54
- *
55
- * @param array $links Plugin links
56
- * @return array Modified plugin links
57
- */
58
- function action_links( $links ) {
59
- $plugin_links = array(
60
- '<a href="' . WOOCOMMERCE_MAILCHIMP_SETTINGS_URL . '">' . __( 'Settings', 'ss_wc_mailchimp' ) . '</a>',
61
- );
62
 
63
- return array_merge( $plugin_links, $links );
64
- }
 
 
65
 
66
- // Add the "Settings" links on the Plugins administration screen
67
- add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'action_links' );
 
 
68
  }
69
- add_action( 'plugins_loaded', 'woocommerce_mailchimp_init', 0 );
 
1
  <?php
2
  /**
3
  * Plugin Name: WooCommerce MailChimp
4
+ * Plugin URI: https://www.saintsystems.com/products/woocommerce-mailchimp/
5
  * Description: WooCommerce MailChimp provides simple MailChimp integration for WooCommerce.
6
+ * Author: Saint Systems
7
+ * Author URI: https://www.saintsystems.com
8
+ * Version: 2.0
9
  * Text Domain: ss_wc_mailchimp
10
  * Domain Path: languages
11
  *
12
+ * Copyright: � 2016 Saint Systems
13
  * License: GNU General Public License v3.0
14
  * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 
 
15
  */
16
 
17
+ /** If this file is called directly, abort. */
18
+ if ( ! defined( 'ABSPATH' ) ) {
19
+ die;
20
+ }
21
 
22
+ /** Constants */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ /**
25
+ * Full path to the WooCommerce MailChimp file
26
+ * @define "SS_WC_MAILCHIMP_FILE" "./woocommmerce-mailchimp.php"
27
+ */
28
+ define( 'SS_WC_MAILCHIMP_FILE', __FILE__ );
29
 
30
+ /**
31
+ * The URL to this file
32
+ */
33
+ define( 'SS_WC_MAILCHIMP_URL', plugin_dir_url( __FILE__ ) );
34
 
35
+ /**
36
+ * The absolute path to the plugin directory
37
+ * @define "SS_WC_MAILCHIMP_DIR" "./"
38
+ */
39
+ define( 'SS_WC_MAILCHIMP_DIR', plugin_dir_path( __FILE__ ) );
40
 
41
+ define( 'SS_WC_MAILCHIMP_PLUGIN_URL', plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) );
 
 
 
 
 
 
 
 
 
42
 
43
+ /**
44
+ * The main plugin class (SS_WC_MailChimp_Plugin)
45
+ */
46
+ require_once( 'includes/class-ss-wc-mailchimp-plugin.php' );
47
 
48
+ /** Register hooks that are fired when the plugin is activated and deactivated. */
49
+ if ( is_admin() ) {
50
+ register_activation_hook( __FILE__, array( 'SS_WC_MailChimp_Plugin', 'activate' ) );
51
+ register_deactivation_hook( __FILE__, array( 'SS_WC_MailChimp_Plugin', 'deactivate' ) );
52
  }
53
+
54
+ add_action( 'plugins_loaded', array( 'SS_WC_MailChimp_Plugin', 'get_instance' ), 0 );