MailChimp Forms by MailMunch - Version 2.1.7

Version Description

Download this release

Release Info

Developer mailmunch
Plugin Icon 128x128 MailChimp Forms by MailMunch
Version 2.1.7
Comparing to
See all releases

Code changes from version 2.1.6 to 2.1.7

admin/class-mailchimp-mailmunch-admin.php CHANGED
@@ -303,7 +303,8 @@ class Mailchimp_Mailmunch_Admin {
303
 
304
  require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/drewm_mailchimp.php';
305
  $mailchimpApi = new DrewmMailChimp(get_option($this->mailmunch_api->getPrefix(). 'mailchimp_access_token'));
306
- $lists = $mailchimpApi->call('lists/list');
 
307
  require_once(plugin_dir_path( __FILE__ ) . 'partials/mailchimp-mailmunch-integrate.php');
308
  break;
309
 
303
 
304
  require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/drewm_mailchimp.php';
305
  $mailchimpApi = new DrewmMailChimp(get_option($this->mailmunch_api->getPrefix(). 'mailchimp_access_token'));
306
+ $rawLists = $mailchimpApi->get('lists', array('fields' => 'lists.id,lists.name,lists.stats.member_count'));
307
+ $lists = $rawLists['lists'];
308
  require_once(plugin_dir_path( __FILE__ ) . 'partials/mailchimp-mailmunch-integrate.php');
309
  break;
310
 
admin/partials/mailchimp-mailmunch-integrate.php CHANGED
@@ -27,12 +27,12 @@
27
  <tr height="50">
28
  <td colspan="3">
29
  <div class="inside-container">
30
- <?php if ($lists['total'] > 0) { ?>
31
  <p>Choose a list to save your subscribers in:</p>
32
  <form action="<?php echo add_query_arg( array('step' => 'final') ); ?>" method="POST">
33
  <select name="list_id">
34
- <?php foreach ($lists['data'] as $list) { ?>
35
- <option value="<?php echo $list['id']; ?>"><?php echo $list['name']; ?></option>
36
  <?php } ?>
37
  </select>
38
  <input type="submit" name="action" value="Choose List" class="button button-primary" />
27
  <tr height="50">
28
  <td colspan="3">
29
  <div class="inside-container">
30
+ <?php if (count($lists) > 0) { ?>
31
  <p>Choose a list to save your subscribers in:</p>
32
  <form action="<?php echo add_query_arg( array('step' => 'final') ); ?>" method="POST">
33
  <select name="list_id">
34
+ <?php foreach ($lists as $list) { ?>
35
+ <option value="<?php echo $list['id']; ?>"><?php echo $list['name']; ?> (<?php echo $list['stats']['member_count'] ?> members)</option>
36
  <?php } ?>
37
  </select>
38
  <input type="submit" name="action" value="Choose List" class="button button-primary" />
includes/class-mailchimp-mailmunch.php CHANGED
@@ -19,7 +19,7 @@ define( 'MAILCHIMP_MAILMUNCH_HOME_URL', "http://app.mailmunch.co" );
19
  define( 'MAILCHIMP_MAILMUNCH_SLUG', "mailchimp-mailmunch" );
20
  define( 'MAILCHIMP_MAILMUNCH_PREFIX', 'mc_mm' );
21
  define( 'MAILCHIMP_MAILMUNCH_PLUGIN_DIRECTORY', 'mailchimp-forms-by-mailmunch' );
22
- define( 'MAILCHIMP_MAILMUNCH_VERSION', '2.1.6' );
23
 
24
  /**
25
  * The core plugin class.
19
  define( 'MAILCHIMP_MAILMUNCH_SLUG', "mailchimp-mailmunch" );
20
  define( 'MAILCHIMP_MAILMUNCH_PREFIX', 'mc_mm' );
21
  define( 'MAILCHIMP_MAILMUNCH_PLUGIN_DIRECTORY', 'mailchimp-forms-by-mailmunch' );
22
+ define( 'MAILCHIMP_MAILMUNCH_VERSION', '2.1.7' );
23
 
24
  /**
25
  * The core plugin class.
includes/drewm_mailchimp.php CHANGED
@@ -1,84 +1,323 @@
1
  <?php
2
  /**
3
- * Super-simple, minimum abstraction MailChimp API v2 wrapper
4
- *
5
- * Uses curl if available, falls back to file_get_contents and HTTP stream.
6
- * This probably has more comments than code.
7
  *
8
- * Contributors:
9
- * Michael Minor <me@pixelbacon.com>
10
- * Lorna Jane Mitchell, github.com/lornajane
11
- *
12
- * @author Drew McLellan <drew.mclellan@gmail.com>
13
- * @version 1.1.1
14
  */
15
  class DrewmMailChimp
16
  {
17
  private $api_key;
18
- private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0';
19
- private $verify_ssl = false;
 
 
 
 
 
 
 
 
 
 
20
 
21
  /**
22
  * Create a new instance
23
  * @param string $api_key Your MailChimp API key
 
24
  */
25
  public function __construct($api_key)
26
  {
27
  $this->api_key = $api_key;
28
- list(, $datacentre) = explode('-', $this->api_key);
29
- $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  }
31
 
32
  /**
33
- * Call an API method. Every request needs the API key, so that is added automatically -- you don't need to pass it in.
34
- * @param string $method The API method to call, e.g. 'lists/list'
35
- * @param array $args An array of arguments to pass to the method. Will be json-encoded for you.
36
- * @return array Associative array of json decoded API response.
 
37
  */
38
- public function call($method, $args = array(), $timeout = 10)
39
  {
40
- return $this->makeRequest($method, $args, $timeout);
41
  }
42
 
43
  /**
44
- * Performs the underlying HTTP request. Not very exciting
 
45
  * @param string $method The API method to be called
46
- * @param array $args Assoc array of parameters to be passed
47
- * @return array Assoc array of decoded result
48
- */
49
- private function makeRequest($method, $args = array(), $timeout = 10)
50
- {
51
- $args['apikey'] = $this->api_key;
52
-
53
- $url = $this->api_endpoint.'/'.$method.'.json';
54
- $json_data = json_encode($args);
55
-
56
- if (function_exists('curl_init') && function_exists('curl_setopt')) {
57
- $ch = curl_init();
58
- curl_setopt($ch, CURLOPT_URL, $url);
59
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
60
- curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
61
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
62
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
63
- curl_setopt($ch, CURLOPT_POST, true);
64
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
65
- curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
66
- $result = curl_exec($ch);
67
- curl_close($ch);
68
- } else {
69
- $result = file_get_contents($url, null, stream_context_create(array(
70
- 'http' => array(
71
- 'protocol_version' => 1.1,
72
- 'user_agent' => 'PHP-MCAPI/2.0',
73
- 'method' => 'POST',
74
- 'header' => "Content-type: application/json\r\n".
75
- "Connection: close\r\n" .
76
- "Content-length: " . strlen($json_data) . "\r\n",
77
- 'content' => $json_data,
78
- ),
79
- )));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  }
81
 
82
- return $result ? json_decode($result, true) : false;
83
  }
84
- }
1
  <?php
2
  /**
3
+ * Super-simple, minimum abstraction MailChimp API v3 wrapper
4
+ * MailChimp API v3: http://developer.mailchimp.com
5
+ * This wrapper: https://github.com/drewm/mailchimp-api
 
6
  *
7
+ * @author Drew McLellan <drew.mclellan@gmail.com>
8
+ * @version 2.2
 
 
 
 
9
  */
10
  class DrewmMailChimp
11
  {
12
  private $api_key;
13
+ private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
14
+
15
+ /* SSL Verification
16
+ Read before disabling:
17
+ http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
18
+ */
19
+ public $verify_ssl = true;
20
+
21
+ private $request_successful = false;
22
+ private $last_error = '';
23
+ private $last_response = array();
24
+ private $last_request = array();
25
 
26
  /**
27
  * Create a new instance
28
  * @param string $api_key Your MailChimp API key
29
+ * @throws \Exception
30
  */
31
  public function __construct($api_key)
32
  {
33
  $this->api_key = $api_key;
34
+
35
+ if (strpos($this->api_key, '-') === false) {
36
+ throw new \Exception("Invalid MailChimp API key `{$api_key}` supplied.");
37
+ }
38
+
39
+ list(, $data_center) = explode('-', $this->api_key);
40
+ $this->api_endpoint = str_replace('<dc>', $data_center, $this->api_endpoint);
41
+
42
+ $this->last_response = array('headers' => null, 'body' => null);
43
+ }
44
+
45
+ /**
46
+ * Create a new instance of a Batch request. Optionally with the ID of an existing batch.
47
+ * @param string $batch_id Optional ID of an existing batch, if you need to check its status for example.
48
+ * @return Batch New Batch object.
49
+ */
50
+ public function new_batch($batch_id = null)
51
+ {
52
+ return new Batch($this, $batch_id);
53
+ }
54
+
55
+ /**
56
+ * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
57
+ * @param string $email The subscriber's email address
58
+ * @return string Hashed version of the input
59
+ */
60
+ public function subscriberHash($email)
61
+ {
62
+ return md5(strtolower($email));
63
+ }
64
+
65
+ /**
66
+ * Was the last request successful?
67
+ * @return bool True for success, false for failure
68
+ */
69
+ public function success()
70
+ {
71
+ return $this->request_successful;
72
+ }
73
+
74
+ /**
75
+ * Get the last error returned by either the network transport, or by the API.
76
+ * If something didn't work, this should contain the string describing the problem.
77
+ * @return array|false describing the error
78
+ */
79
+ public function getLastError()
80
+ {
81
+ return $this->last_error ?: false;
82
+ }
83
+
84
+ /**
85
+ * Get an array containing the HTTP headers and the body of the API response.
86
+ * @return array Assoc array with keys 'headers' and 'body'
87
+ */
88
+ public function getLastResponse()
89
+ {
90
+ return $this->last_response;
91
+ }
92
+
93
+ /**
94
+ * Get an array containing the HTTP headers and the body of the API request.
95
+ * @return array Assoc array
96
+ */
97
+ public function getLastRequest()
98
+ {
99
+ return $this->last_request;
100
+ }
101
+
102
+ /**
103
+ * Make an HTTP DELETE request - for deleting data
104
+ * @param string $method URL of the API request method
105
+ * @param array $args Assoc array of arguments (if any)
106
+ * @param int $timeout Timeout limit for request in seconds
107
+ * @return array|false Assoc array of API response, decoded from JSON
108
+ */
109
+ public function delete($method, $args = array(), $timeout = 10)
110
+ {
111
+ return $this->makeRequest('delete', $method, $args, $timeout);
112
+ }
113
+
114
+ /**
115
+ * Make an HTTP GET request - for retrieving data
116
+ * @param string $method URL of the API request method
117
+ * @param array $args Assoc array of arguments (usually your data)
118
+ * @param int $timeout Timeout limit for request in seconds
119
+ * @return array|false Assoc array of API response, decoded from JSON
120
+ */
121
+ public function get($method, $args = array(), $timeout = 10)
122
+ {
123
+ return $this->makeRequest('get', $method, $args, $timeout);
124
+ }
125
+
126
+ /**
127
+ * Make an HTTP PATCH request - for performing partial updates
128
+ * @param string $method URL of the API request method
129
+ * @param array $args Assoc array of arguments (usually your data)
130
+ * @param int $timeout Timeout limit for request in seconds
131
+ * @return array|false Assoc array of API response, decoded from JSON
132
+ */
133
+ public function patch($method, $args = array(), $timeout = 10)
134
+ {
135
+ return $this->makeRequest('patch', $method, $args, $timeout);
136
+ }
137
+
138
+ /**
139
+ * Make an HTTP POST request - for creating and updating items
140
+ * @param string $method URL of the API request method
141
+ * @param array $args Assoc array of arguments (usually your data)
142
+ * @param int $timeout Timeout limit for request in seconds
143
+ * @return array|false Assoc array of API response, decoded from JSON
144
+ */
145
+ public function post($method, $args = array(), $timeout = 10)
146
+ {
147
+ return $this->makeRequest('post', $method, $args, $timeout);
148
  }
149
 
150
  /**
151
+ * Make an HTTP PUT request - for creating new items
152
+ * @param string $method URL of the API request method
153
+ * @param array $args Assoc array of arguments (usually your data)
154
+ * @param int $timeout Timeout limit for request in seconds
155
+ * @return array|false Assoc array of API response, decoded from JSON
156
  */
157
+ public function put($method, $args = array(), $timeout = 10)
158
  {
159
+ return $this->makeRequest('put', $method, $args, $timeout);
160
  }
161
 
162
  /**
163
+ * Performs the underlying HTTP request. Not very exciting.
164
+ * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete
165
  * @param string $method The API method to be called
166
+ * @param array $args Assoc array of parameters to be passed
167
+ * @param int $timeout
168
+ * @return array|false Assoc array of decoded result
169
+ * @throws \Exception
170
+ */
171
+ private function makeRequest($http_verb, $method, $args = array(), $timeout = 10)
172
+ {
173
+ if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
174
+ throw new \Exception("cURL support is required, but can't be found.");
175
+ }
176
+
177
+ $url = $this->api_endpoint . '/' . $method;
178
+
179
+ $this->last_error = '';
180
+ $this->request_successful = false;
181
+ $response = array('headers' => null, 'body' => null);
182
+ $this->last_response = $response;
183
+
184
+ $this->last_request = array(
185
+ 'method' => $http_verb,
186
+ 'path' => $method,
187
+ 'url' => $url,
188
+ 'body' => '',
189
+ 'timeout' => $timeout,
190
+ );
191
+
192
+ $ch = curl_init();
193
+ curl_setopt($ch, CURLOPT_URL, $url);
194
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
195
+ 'Accept: application/vnd.api+json',
196
+ 'Content-Type: application/vnd.api+json',
197
+ 'Authorization: apikey ' . $this->api_key
198
+ ));
199
+ curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)');
200
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
201
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
202
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
203
+ curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
204
+ curl_setopt($ch, CURLOPT_ENCODING, '');
205
+ curl_setopt($ch, CURLINFO_HEADER_OUT, true);
206
+
207
+ switch ($http_verb) {
208
+ case 'post':
209
+ curl_setopt($ch, CURLOPT_POST, true);
210
+ $this->attachRequestPayload($ch, $args);
211
+ break;
212
+
213
+ case 'get':
214
+ $query = http_build_query($args, '', '&');
215
+ curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
216
+ break;
217
+
218
+ case 'delete':
219
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
220
+ break;
221
+
222
+ case 'patch':
223
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
224
+ $this->attachRequestPayload($ch, $args);
225
+ break;
226
+
227
+ case 'put':
228
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
229
+ $this->attachRequestPayload($ch, $args);
230
+ break;
231
+ }
232
+
233
+ $response['body'] = curl_exec($ch);
234
+ $response['headers'] = curl_getinfo($ch);
235
+
236
+ if (isset($response['headers']['request_header'])) {
237
+ $this->last_request['headers'] = $response['headers']['request_header'];
238
+ }
239
+
240
+ if ($response['body'] === false) {
241
+ $this->last_error = curl_error($ch);
242
+ }
243
+
244
+ curl_close($ch);
245
+
246
+ $formattedResponse = $this->formatResponse($response);
247
+
248
+ $this->determineSuccess($response, $formattedResponse);
249
+
250
+ return $formattedResponse;
251
+ }
252
+
253
+ /**
254
+ * Encode the data and attach it to the request
255
+ * @param resource $ch cURL session handle, used by reference
256
+ * @param array $data Assoc array of data to attach
257
+ */
258
+ private function attachRequestPayload(&$ch, $data)
259
+ {
260
+ $encoded = json_encode($data);
261
+ $this->last_request['body'] = $encoded;
262
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
263
+ }
264
+
265
+ /**
266
+ * Decode the response and format any error messages for debugging
267
+ * @param array $response The response from the curl request
268
+ * @return array|false The JSON decoded into an array
269
+ */
270
+ private function formatResponse($response)
271
+ {
272
+ $this->last_response = $response;
273
+
274
+ if (!empty($response['body'])) {
275
+ return json_decode($response['body'], true);
276
+ }
277
+
278
+ return false;
279
+ }
280
+
281
+ /**
282
+ * Check if the response was successful or a failure. If it failed, store the error.
283
+ * @param array $response The response from the curl request
284
+ * @param array|false $formattedResponse The response body payload from the curl request
285
+ * @return bool If the request was successful
286
+ */
287
+ private function determineSuccess($response, $formattedResponse)
288
+ {
289
+ $status = $this->findHTTPStatus($response, $formattedResponse);
290
+
291
+ if ($status >= 200 && $status <= 299) {
292
+ $this->request_successful = true;
293
+ return true;
294
+ }
295
+
296
+ if (isset($formattedResponse['detail'])) {
297
+ $this->last_error = sprintf('%d: %s', $formattedResponse['status'], $formattedResponse['detail']);
298
+ return false;
299
+ }
300
+
301
+ $this->last_error = 'Unknown error, call getLastResponse() to find out what happened.';
302
+ return false;
303
+ }
304
+
305
+ /**
306
+ * Find the HTTP status code from the headers or API response body
307
+ * @param array $response The response from the curl request
308
+ * @param array|false $formattedResponse The response body payload from the curl request
309
+ * @return int HTTP status code
310
+ */
311
+ private function findHTTPStatus($response, $formattedResponse)
312
+ {
313
+ if (!empty($response['headers']) && isset($response['headers']['http_code'])) {
314
+ return (int) $response['headers']['http_code'];
315
+ }
316
+
317
+ if (!empty($response['body']) && isset($formattedResponse['status'])) {
318
+ return (int) $formattedResponse['status'];
319
  }
320
 
321
+ return 418;
322
  }
323
+ }
mailchimp-mailmunch.php CHANGED
@@ -16,7 +16,7 @@
16
  * Plugin Name: MailChimp Forms by MailMunch
17
  * Plugin URI: http://connect.mailchimp.com/integrations/mailmunch-email-list-builder
18
  * Description: The MailChimp plugin allows you to quickly and easily add signup forms for your MailChimp lists. Popup, Embedded, Top Bar and a variety of different options available.
19
- * Version: 2.1.6
20
  * Author: MailMunch
21
  * Author URI: http://www.mailmunch.co
22
  * License: GPL-2.0+
16
  * Plugin Name: MailChimp Forms by MailMunch
17
  * Plugin URI: http://connect.mailchimp.com/integrations/mailmunch-email-list-builder
18
  * Description: The MailChimp plugin allows you to quickly and easily add signup forms for your MailChimp lists. Popup, Embedded, Top Bar and a variety of different options available.
19
+ * Version: 2.1.7
20
  * Author: MailMunch
21
  * Author URI: http://www.mailmunch.co
22
  * License: GPL-2.0+
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === MailChimp Forms by MailMunch ===
2
- Contributors: mailmunch, lizgannes, MailChimp Forms by MailMunch
3
  Tags: mailchimp, MailChimp form, MailChimp plugin, mailchimp popup, mailchimp newsletter, mailchimp widget, mailchimp wordpress
4
 
5
  Requires at least: 3.8
6
  Tested up to: 4.7
7
- Stable tag: 2.1.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -31,6 +31,7 @@ Our improved signup plugin for MailChimp will proactively sign-up new subscriber
31
  * MailChimp Sidebar Widget
32
  * MailChimp Top Bar
33
  * MailChimp Scroll Box
 
34
 
35
  = MailChimp Forms - Features =
36
  * Responsive Design: Your MailChimp forms will look beautiful on any device, mobile, desktop or tablet
@@ -188,6 +189,9 @@ If you have been looking for the best email marketing software programs, look no
188
 
189
  == Changelog ==
190
 
 
 
 
191
  = MailChimp Forms (2016-07-28) =
192
  * Added new MailChimp themes
193
  * Added date field and textarea field
1
  === MailChimp Forms by MailMunch ===
2
+ Contributors: mailmunch, lizgannes
3
  Tags: mailchimp, MailChimp form, MailChimp plugin, mailchimp popup, mailchimp newsletter, mailchimp widget, mailchimp wordpress
4
 
5
  Requires at least: 3.8
6
  Tested up to: 4.7
7
+ Stable tag: 2.1.7
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
31
  * MailChimp Sidebar Widget
32
  * MailChimp Top Bar
33
  * MailChimp Scroll Box
34
+ * MailChimp Landing Pages
35
 
36
  = MailChimp Forms - Features =
37
  * Responsive Design: Your MailChimp forms will look beautiful on any device, mobile, desktop or tablet
189
 
190
  == Changelog ==
191
 
192
+ = MailChimp Forms 2.1.7 =
193
+ * Updated to use MailChimp API v3
194
+
195
  = MailChimp Forms (2016-07-28) =
196
  * Added new MailChimp themes
197
  * Added date field and textarea field