PluginProbe
Mailchimp List Subscribe Form / 1.2.6
Mailchimp List Subscribe Form v1.2.6
1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 All 55 releases
mailchimp / mailchimp.php

mailchimp.php in Mailchimp List Subscribe Form 1.2.6, at mailchimp.php

1,399 lines 45.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: MailChimp
4 Plugin URI: http://www.mailchimp.com/plugins/mailchimp-wordpress-plugin/
5 Description: The MailChimp plugin allows you to quickly and easily add a signup form for your MailChimp list.
6 Version: 1.2.6
7 Author: MailChimp and Crowd Favorite
8 Author URI: http://mailchimp.com/api/
9 */
10 /* Copyright 2008 MailChimp.com (email : api@mailchimp.com)
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 // Version constant for easy CSS refreshes
28 define('MCSF_VER', '1.2.6');
29
30 // What's our permission (capability) threshold
31 define('MCSF_CAP_THRESHOLD', 'manage_options');
32
33 // Define our location constants, both MCSF_DIR and MCSF_URL
34 mailchimpSF_where_am_i();
35
36 // Get our MailChimp API class in scope
37 if (!class_exists('mailchimpSF_MCAPI')) {
38 require_once('miniMCAPI.class.php');
39 }
40
41 // includes the widget code so it can be easily called either normally or via ajax
42 include_once('mailchimp_widget.php');
43
44 // includes the backwards compatibility functions
45 include_once('mailchimp_compat.php');
46
47 /**
48 * Do the following plugin setup steps here
49 *
50 * Internationalization
51 * Resource (JS & CSS) enqueuing
52 *
53 * @return void
54 */
55 function mailchimpSF_plugin_init() {
56 // Internationalize the plugin
57 $textdomain = 'mailchimp_i18n';
58 $locale = apply_filters( 'plugin_locale', get_locale(), $textdomain);
59 load_textdomain('mailchimp_i18n', MCSF_LANG_DIR.$textdomain.'-'.$locale.'.mo');
60
61 // Bring in our appropriate JS and CSS resources
62 mailchimpSF_load_resources();
63 }
64 add_action( 'init', 'mailchimpSF_plugin_init' );
65
66
67 /**
68 * Add the settings link to the MailChimp plugin row
69 *
70 * @param array $links - Links for the plugin
71 * @return array - Links
72 */
73 function mailchimpSD_plugin_action_links($links) {
74 $settings_page = add_query_arg(array('page' => 'mailchimpSF_options'), admin_url('options-general.php'));
75 $settings_link = '<a href="'.esc_url($settings_page).'">'.__('Settings', 'mailchimp_i18n' ).'</a>';
76 array_unshift($links, $settings_link);
77 return $links;
78 }
79 add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mailchimpSD_plugin_action_links', 10, 1);
80
81 /**
82 * Loads the appropriate JS and CSS resources depending on
83 * settings and context (admin or not)
84 *
85 * @return void
86 */
87 function mailchimpSF_load_resources() {
88 // JS
89 if (get_option('mc_use_javascript') == 'on') {
90 if (!is_admin()) {
91 wp_enqueue_script('jquery_scrollto', MCSF_URL.'js/scrollTo.js', array('jquery'), MCSF_VER);
92 wp_enqueue_script('mailchimpSF_main_js', MCSF_URL.'js/mailchimp.js', array('jquery', 'jquery-form'), MCSF_VER);
93 // some javascript to get ajax version submitting to the proper location
94 global $wp_scripts;
95 $wp_scripts->localize('mailchimpSF_main_js', 'mailchimpSF', array(
96 'ajax_url' => trailingslashit(home_url()),
97 ));
98 }
99 }
100
101 if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
102 // Datepicker theme
103 wp_enqueue_style('flick', MCSF_URL.'/css/flick/flick.css');
104 // Datepicker JS
105 wp_enqueue_script('datepicker', MCSF_URL.'/js/datepicker.js', array('jquery','jquery-ui-core'));
106 }
107
108 // CSS
109 else {
110 wp_enqueue_style('mailchimpSF_main_css', home_url('?mcsf_action=main_css&ver='.MCSF_VER));
111 wp_enqueue_style('mailchimpSF_ie_css', MCSF_URL.'css/ie.css');
112 global $wp_styles;
113 $wp_styles->add_data( 'mailchimpSF_ie_css', 'conditional', 'IE' );
114 }
115 }
116
117
118 /**
119 * Loads resources for the MailChimp admin page
120 *
121 * @return void
122 */
123 function mc_admin_page_load_resources() {
124 wp_enqueue_style('mailchimpSF_admin_css', MCSF_URL.'css/admin.css');
125 }
126 add_action('load-settings_page_mailchimpSF_options', 'mc_admin_page_load_resources');
127
128
129 /**
130 * Loads jQuery Datepicker for the date-pick class
131 **/
132 function mc_datepicker_load() {
133 ?>
134 <script type="text/javascript">
135 jQuery(function($) {
136 $('.date-pick').datepicker({
137 autoFocusNextInput: true,
138 constrainInput: false,
139 changeMonth: true,
140 changeYear: true,
141 beforeShow: function(input, inst) { $('#ui-datepicker-div').addClass('show'); },
142 dateFormat: 'yy/mm/dd',
143 });
144
145 d = new Date();
146 $('.birthdate-pick').datepicker({
147 autoFocusNextInput: true,
148 constrainInput: false,
149 changeMonth: true,
150 changeYear: false,
151 minDate: new Date(d.getFullYear(), 1-1, 1),
152 maxDate: new Date(d.getFullYear(), 12-1, 31),
153 beforeShow: function(input, inst) { $('#ui-datepicker-div').removeClass('show'); },
154 dateFormat: 'mm/dd',
155 });
156
157
158 });
159 </script>
160 <?php
161 }
162 if (get_option('mc_use_datepicker') == 'on' && !is_admin()) {
163 add_action('wp_head', 'mc_datepicker_load');
164 }
165
166 /**
167 * Handles requests that as light-weight a load as possible.
168 * typically, JS or CSS
169 **/
170 function mailchimpSF_early_request_handler() {
171 if (isset($_GET['mcsf_action'])) {
172 switch ($_GET['mcsf_action']) {
173 case 'main_css':
174 header("Content-type: text/css");
175 mailchimpSF_main_css();
176 exit;
177 }
178 }
179 }
180 add_action('init', 'mailchimpSF_early_request_handler', 0);
181
182 /**
183 * Outputs the front-end CSS. This checks several options, so it
184 * was best to put it in a Request-handled script, as opposed to
185 * a static file.
186 */
187 function mailchimpSF_main_css() {
188 ?>
189 .mc_error_msg {
190 color: red;
191 }
192 .mc_success_msg {
193 color: green;
194 }
195 .mc_merge_var{
196 padding:0;
197 margin:0;
198 }
199 <?php
200 // If we're utilizing custom styles
201 if (get_option('mc_custom_style')=='on'){
202 ?>
203 #mc_signup_form {
204 padding:5px;
205 border-width: <?php echo get_option('mc_form_border_width'); ?>px;
206 border-style: <?php echo (get_option('mc_form_border_width')==0) ? 'none' : 'solid'; ?>;
207 border-color: #<?php echo get_option('mc_form_border_color'); ?>;
208 color: #<?php echo get_option('mc_form_text_color'); ?>;
209 background-color: #<?php echo get_option('mc_form_background'); ?>;
210 }
211
212
213 .mc_custom_border_hdr {
214 border-width: <?php echo get_option('mc_header_border_width'); ?>px;
215 border-style: <?php echo (get_option('mc_header_border_width')==0) ? 'none' : 'solid'; ?>;
216 border-color: #<?php echo get_option('mc_header_border_color'); ?>;
217 color: #<?php echo get_option('mc_header_text_color'); ?>;
218 background-color: #<?php echo get_option('mc_header_background'); ?>;
219 font-size: 1.2em;
220 padding:5px 10px;
221 width: 100%;
222 }
223 <?php
224 }
225 ?>
226 #mc_signup_container {}
227 #mc_signup_form {}
228 #mc_signup_form .mc_var_label {}
229 #mc_signup_form .mc_input {}
230 #mc-indicates-required {
231 width:100%;
232 }
233 #mc_display_rewards {}
234 .mc_interests_header {
235 font-weight:bold;
236 }
237 div.mc_interest{
238 width:100%;
239 }
240 #mc_signup_form input.mc_interest {}
241 #mc_signup_form select {}
242 #mc_signup_form label.mc_interest_label {
243 display:inline;
244 }
245 .mc_signup_submit {
246 text-align:center;
247 }
248 ul.mc_list {
249 list-style-type: none;
250 }
251 ul.mc_list li {
252 font-size: 12px;
253 }
254 .ui-datepicker-year {
255 display: none;
256 }
257 #ui-datepicker-div.show .ui-datepicker-year {
258 display: inline;
259 padding-left: 3px
260 }
261 <?php
262 }
263
264
265 /**
266 * Add our settings page to the admin menu
267 *
268 * @return void
269 */
270 function mailchimpSF_add_pages(){
271 // Add settings page for users who can edit plugins
272 add_options_page( __( 'MailChimp Setup', 'mailchimp_i18n' ), __( 'MailChimp Setup', 'mailchimp_i18n' ), MCSF_CAP_THRESHOLD, 'mailchimpSF_options', 'mailchimpSF_setup_page');
273 }
274 add_action('admin_menu', 'mailchimpSF_add_pages');
275
276 function mailchimpSF_request_handler() {
277 if (isset($_POST['mcsf_action'])) {
278 switch ($_POST['mcsf_action']) {
279 case 'logout':
280 // Check capability & Verify nonce
281 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'mc_logout')) {
282 wp_die('Cheatin&rsquo; huh?');
283 }
284
285 // erase API Key
286 update_option('mc_apikey', '');
287 break;
288 case 'update_mc_apikey':
289 // Check capability & Verify nonce
290 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_mc_api_key')) {
291 wp_die('Cheatin&rsquo; huh?');
292 }
293
294 mailchimpSF_set_api_key(strip_tags(stripslashes($_POST['mc_apikey'])));
295 break;
296 case 'reset_list':
297 // Check capability & Verify nonce
298 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'reset_mailchimp_list')) {
299 wp_die('Cheatin&rsquo; huh?');
300 }
301
302 mailchimpSF_reset_list_settings();
303 break;
304 case 'change_form_settings':
305 if (!current_user_can(MCSF_CAP_THRESHOLD) || !wp_verify_nonce($_POST['_mcsf_nonce_action'], 'update_general_form_settings')) {
306 wp_die('Cheatin&rsquo; huh?');
307 }
308
309 // Update the form settings
310 mailchimpSF_save_general_form_settings();
311 break;
312 case 'mc_submit_signup_form':
313 // Validate nonce
314 if (!wp_verify_nonce($_POST['_mc_submit_signup_form_nonce'], 'mc_submit_signup_form')) {
315 wp_die('Cheatin&rsquo; huh?');
316 }
317
318 // Attempt the signup
319 mailchimpSF_signup_submit();
320
321 // Do a different action for html vs. js
322 switch ($_POST['mc_submit_type']) {
323 case 'html':
324 /* Allow to fall through. The widget will pick up the
325 * global message left over from the signup_submit function */
326 break;
327 case 'js':
328 if (!headers_sent()){ //just in case...
329 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT', true, 200);
330 }
331 echo mailchimpSF_global_msg(); // Don't esc_html this, b/c we've already escaped it
332 exit;
333 }
334 }
335 }
336 }
337 add_action('init', 'mailchimpSF_request_handler');
338
339 /**
340 * Upgrades data if it needs to. Checks on admin_init
341 *
342 * @return void
343 */
344 function mailchimpSF_upgrade() {
345 // See if we need an upgrade
346 if (mailchimpSF_needs_upgrade()) {
347 // remove password option if it's set (0.5)
348 // Update interest group data
349 mailchimpSF_do_upgrade();
350 }
351 }
352 add_action('admin_init', 'mailchimpSF_upgrade');
353
354 /**
355 * Checks to see if we're storing a password, if so, we need
356 * to upgrade to the API key
357 *
358 * @return bool
359 **/
360 function mailchimpSF_needs_upgrade() {
361 $igs = get_option('mc_interest_groups');
362
363 if ($igs !== false // we have an option
364 && (
365 empty($igs) || // it can be an empty array (no interest groups)
366 (is_array($igs) && isset($igs[0]['id'])) // OR it should be a populated array that's well-formed
367 )) {
368 return false; // no need to upgrade
369 }
370 else {
371 return true; // yeah, let's do it
372 }
373 }
374
375 /**
376 * 1.2.4 -> 1.2.5 - Update to support multiple interest groups
377 * MCAPIv1.2 -> MCAPIv1.3 - update interest groups
378 * 2011-02-09 - old password upgrade code deleted as 0.5 is way old
379 */
380 function mailchimpSF_do_upgrade() {
381 //left in just for good measure
382 delete_option('mc_password');
383 $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
384 $igs = $api->listInterestGroupings(get_option('mc_list_id'));
385
386 // If we don't have any interest groups store an empty array, not (bool) false
387 $igs = !$igs ? array() : $igs;
388
389 update_option('mc_interest_groups', $igs);
390 }
391
392 /**
393 * Sets the API Key to whatever value was passed to this func
394 *
395 * @return array of vars
396 **/
397 function mailchimpSF_set_api_key($api_key = '') {
398 $delete_setup = false;
399 $api = new mailchimpSF_MCAPI($api_key);
400 $api->ping();
401 if (empty($api->errorCode)) {
402 $msg = "<p class='success_msg'>".esc_html(__("Success! We were able to verify your API Key! Let's continue, shall we?", 'mailchimp_i18n'))."</p>";
403 update_option('mc_apikey', $api_key);
404 $req = $api->getAccountDetails();
405 update_option('mc_username', $req['username']);
406 update_option('mc_user_id', $req['user_id']);
407 $cur_list_id = get_option('mc_list_id');
408 if (!empty($cur_list_id)) {
409 //we *could* support paging, but few users have that many lists (and shouldn't)
410 $lists = $api->lists(array(),0,100);
411 $lists = $lists['data'];
412 //but don't delete if the list still exists...
413 $delete_setup = true;
414 foreach($lists as $list) {
415 if ($list['id'] == $cur_list_id) {
416 $list_id = isset($_POST['mc_list_id']) ? $_POST['mc_list_id'] : '';
417 $delete_setup = false;
418 }
419 }
420 }
421 } else {
422 $msg = "<p class='error_msg'>".esc_html(__('Uh-oh, we were unable to verify your API Key. Please check them and try again!', 'mailchimp_i18n'))."<br/>";
423 $msg .= __('The server said:', 'mailchimp_i18n')."<em>".esc_html($api->errorMessage)."</em></p>";
424 $username = get_option('mc_username');
425 if (empty($username)) {
426 $delete_setup = true;
427 }
428 }
429
430 // Set a global message
431 mailchimpSF_global_msg($msg);
432
433 // If we need to delete our setup, do it
434 if ($delete_setup){
435 mailchimpSF_delete_setup();
436 }
437 }
438
439 /**
440 * Deletes all mailchimp options
441 **/
442 function mailchimpSF_delete_setup() {
443 delete_option('mc_user_id');
444 delete_option('mc_rewards');
445 delete_option('mc_use_javascript');
446 delete_option('mc_use_datepicker');
447 delete_option('mc_use_unsub_link');
448 delete_option('mc_list_id');
449 delete_option('mc_list_name');
450 $igs = get_option('mc_interest_groups');
451 if (is_array($igs)) {
452 foreach ($igs as $ig) {
453 $opt = 'mc_show_interest_groups_'.$ig['id'];
454 delete_option($opt);
455 }
456 }
457 delete_option('mc_interest_groups');
458 $mv = get_option('mc_merge_vars');
459 if (is_array($mv)){
460 foreach($mv as $var){
461 $opt = 'mc_mv_'.$var['tag'];
462 delete_option($opt);
463 }
464 }
465 delete_option('mc_merge_vars');
466 }
467
468 /**
469 * Resets the list settings, there's only one list
470 * that can have settings at a time, so no list_id
471 * parameter is necessary.
472 **/
473 function mailchimpSF_reset_list_settings() {
474
475 delete_option('mc_list_id');
476 delete_option('mc_list_name');
477 delete_option('mc_merge_vars');
478 delete_option('mc_interest_groups');
479
480 delete_option('mc_use_javascript');
481 delete_option('mc_use_unsub_link');
482 delete_option('mc_use_datepicker');
483
484 delete_option('mc_header_content');
485 delete_option('mc_subheader_content');
486 delete_option('mc_submit_text');
487
488 delete_option('mc_custom_style');
489
490 delete_option('mc_header_border_width');
491 delete_option('mc_header_border_color');
492 delete_option('mc_header_background');
493 delete_option('mc_header_text_color');
494
495 delete_option('mc_form_border_width');
496 delete_option('mc_form_border_color');
497 delete_option('mc_form_background');
498 delete_option('mc_form_text_color');
499
500 $msg = '<p class="success_msg">'.esc_html(__('Successfully Reset your List selection... Now you get to pick again!', 'mailchimp_i18n')).'</p>';
501 mailchimpSF_global_msg($msg);
502 }
503
504 /**
505 * Gets or sets a global message based on parameter passed to it
506 *
507 * @return string/bool depending on get/set
508 **/
509 function mailchimpSF_global_msg($msg = null) {
510 global $mcsf_msgs;
511
512 // Make sure we're formed properly
513 if (!is_array($mcsf_msgs)) {
514 $mcsf_msgs = array();
515 }
516
517 // See if we're getting
518 if (is_null($msg)) {
519 return implode('', $mcsf_msgs);
520 }
521
522 // Must be setting
523 $mcsf_msgs[] = $msg;
524 return true;
525 }
526
527 /**
528 * Sets the default options for the option form
529 **/
530 function mailchimpSF_set_form_defaults($list_name = '') {
531 update_option('mc_header_content',__( 'Sign up for', 'mailchimp_i18n' ).' '.$list_name);
532 update_option('mc_submit_text',__( 'Subscribe', 'mailchimp_i18n' ));
533
534 update_option('mc_use_datepicker', 'on');
535 update_option('mc_custom_style','on');
536 update_option('mc_use_javascript','on');
537 update_option('mc_use_unsub_link','off');
538 update_option('mc_header_border_width','1');
539 update_option('mc_header_border_color','E3E3E3');
540 update_option('mc_header_background','FFFFFF');
541 update_option('mc_header_text_color','CC6600');
542
543 update_option('mc_form_border_width','1');
544 update_option('mc_form_border_color','C4D3EA');
545 update_option('mc_form_background','EEF3F8');
546 update_option('mc_form_text_color','555555');
547 }
548
549 /**
550 * Saves the General Form settings on the options page
551 *
552 * @return void
553 **/
554 function mailchimpSF_save_general_form_settings() {
555 if (isset($_POST['mc_rewards'])){
556 update_option('mc_rewards', 'on');
557 $msg = '<p class="success_msg">'.__('Monkey Rewards turned On!', 'mailchimp_i18n').'</p>';
558 mailchimpSF_global_msg($msg);
559 } else if (get_option('mc_rewards')!='off') {
560 update_option('mc_rewards', 'off');
561 $msg = '<p class="success_msg">'.__('Monkey Rewards turned Off!', 'mailchimp_i18n').'</p>';
562 mailchimpSF_global_msg($msg);
563 }
564 if (isset($_POST['mc_use_javascript'])){
565 update_option('mc_use_javascript', 'on');
566 $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned On!', 'mailchimp_i18n').'</p>';
567 mailchimpSF_global_msg($msg);
568 } else if (get_option('mc_use_javascript')!='off') {
569 update_option('mc_use_javascript', 'off');
570 $msg = '<p class="success_msg">'.__('Fancy Javascript submission turned Off!', 'mailchimp_i18n').'</p>';
571 mailchimpSF_global_msg($msg);
572 }
573
574 if (isset($_POST['mc_use_datepicker'])){
575 update_option('mc_use_datepicker', 'on');
576 $msg = '<p class="success_msg">'.__('Datepicker turned On!', 'mailchimp_i18n').'</p>';
577 mailchimpSF_global_msg($msg);
578 } else if (get_option('mc_use_datepicker')!='off') {
579 update_option('mc_use_datepicker', 'off');
580 $msg = '<p class="success_msg">'.__('Datepicker turned Off!', 'mailchimp_i18n').'</p>';
581 mailchimpSF_global_msg($msg);
582 }
583
584 if (isset($_POST['mc_use_unsub_link'])){
585 update_option('mc_use_unsub_link', 'on');
586 $msg = '<p class="success_msg">'.__('Unsubscribe link turned On!', 'mailchimp_i18n').'</p>';
587 mailchimpSF_global_msg($msg);
588 } else if (get_option('mc_use_unsub_link')!='off') {
589 update_option('mc_use_unsub_link', 'off');
590 $msg = '<p class="success_msg">'.__('Unsubscribe link turned Off!', 'mailchimp_i18n').'</p>';
591 mailchimpSF_global_msg($msg);
592 }
593
594 $content = stripslashes($_POST['mc_header_content']);
595 $content = str_replace("\r\n","<br/>", $content);
596 update_option('mc_header_content', $content );
597
598 $content = stripslashes($_POST['mc_subheader_content']);
599 $content = str_replace("\r\n","<br/>", $content);
600 update_option('mc_subheader_content', $content );
601
602
603 $submit_text = stripslashes($_POST['mc_submit_text']);
604 $submit_text = str_replace("\r\n","", $submit_text);
605 update_option('mc_submit_text', $submit_text);
606
607 // Set Custom Style option
608 update_option('mc_custom_style', isset($_POST['mc_custom_style']) ? 'on' : 'off');
609
610 //we told them not to put these things we are replacing in, but let's just make sure they are listening...
611 update_option('mc_header_border_width',str_replace('px','',$_POST['mc_header_border_width']) );
612 update_option('mc_header_border_color', str_replace('#','',$_POST['mc_header_border_color']));
613 update_option('mc_header_background',str_replace('#','',$_POST['mc_header_background']));
614 update_option('mc_header_text_color', str_replace('#','',$_POST['mc_header_text_color']));
615
616 update_option('mc_form_border_width',str_replace('px','',$_POST['mc_form_border_width']) );
617 update_option('mc_form_border_color', str_replace('#','',$_POST['mc_form_border_color']));
618 update_option('mc_form_background',str_replace('#','',$_POST['mc_form_background']));
619 update_option('mc_form_text_color', str_replace('#','',$_POST['mc_form_text_color']));
620
621 $igs = get_option('mc_interest_groups');
622 if (is_array($igs)) {
623 foreach($igs as $var){
624 $opt = 'mc_show_interest_groups_'.$var['id'];
625 if (isset($_POST[$opt])){
626 update_option($opt,'on');
627 } else {
628 update_option($opt,'off');
629 }
630 }
631 }
632
633 $mv = get_option('mc_merge_vars');
634 if (is_array($mv)) {
635 foreach($mv as $var){
636 $opt = 'mc_mv_'.$var['tag'];
637 if (isset($_POST[$opt]) || $var['req']=='Y'){
638 update_option($opt,'on');
639 } else {
640 update_option($opt,'off');
641 }
642 }
643 }
644 $msg = '<p class="success_msg">'.esc_html(__('Successfully Updated your List Subscribe Form Settings!', 'mailchimp_i18n')).'</p>';
645 mailchimpSF_global_msg($msg);
646 }
647
648 /**
649 * Sees if the user changed the list, and updates options accordingly
650 **/
651 function mailchimpSF_change_list_if_necessary($api_key) {
652 // Simple permission check before going through all this
653 if (!current_user_can(MCSF_CAP_THRESHOLD)) { return; }
654
655 $api = new mailchimpSF_MCAPI($api_key);
656 //we *could* support paging, but few users have that many lists (and shouldn't)
657 $lists = $api->lists(array(),0,100);
658 $lists = $lists['data'];
659
660 if (is_array($lists) && !empty($lists) && isset($_POST['mc_list_id'])) {
661
662 /* If our incoming list ID (the one chosen in the select dropdown)
663 is in our array of lists, the set it to be the active list */
664 foreach($lists as $key => $list) {
665 if ($list['id'] == $_POST['mc_list_id']) {
666 $list_id = $_POST['mc_list_id'];
667 $list_name = $list['name'];
668 $list_key = $key;
669 }
670 }
671
672 $orig_list = get_option('mc_list_id');
673 if ($list_id != '') {
674 update_option('mc_list_id', $list_id);
675 update_option('mc_list_name', $list_name);
676 update_option('mc_email_type_option', $lists[$list_key]['email_type_option']);
677
678
679 // See if the user changed the list
680 if ($orig_list != $list_id){
681 // The user changed the list, Reset the Form Defaults
682 mailchimpSF_set_form_defaults($list_name);
683 }
684 // email_type_option
685
686 // Grab the merge vars and interest groups
687 $mv = $api->listMergeVars($list_id);
688 $igs = $api->listInterestGroupings($list_id);
689
690 update_option('mc_merge_vars', $mv);
691 foreach($mv as $var){
692 $opt = 'mc_mv_'.$var['tag'];
693 //turn them all on by default
694 if ($orig_list != $list_id) {
695 update_option($opt, 'on' );
696 }
697 }
698 update_option('mc_interest_groups', $igs);
699 if (is_array($igs)) {
700 foreach ($igs as $var){
701 $opt = 'mc_show_interest_groups_'.$var['id'];
702 //turn them all on by default
703 if ($orig_list != $list_id) {
704 update_option($opt, 'on' );
705 }
706 }
707 }
708 $igs_text = ' ';
709 if (is_array($igs)) {
710 $igs_text .= sprintf(__('and %s Sets of Interest Groups', 'mailchimp_i18n'), count($igs));
711 }
712
713 $msg = '<p class="success_msg">'.
714 sprintf(
715 __('Success! Loaded and saved the info for %d Merge Variables', 'mailchimp_i18n').$igs_text,
716 count($mv)
717 ).' '.
718 __('from your list').' "'.$list_name.'"<br/><br/>'.
719 __('Now you should either Turn On the MailChimp Widget or change your options below, then turn it on.', 'mailchimp_i18n').'</p>';
720 mailchimpSF_global_msg($msg);
721 }
722 }
723 }
724
725 /**
726 * Outputs the Settings/Options page
727 */
728 function mailchimpSF_setup_page() {
729 ?>
730 <div class="wrap">
731
732 <h2><?php esc_html_e('MailChimp List Setup', 'mailchimp_i18n');?> </h2>
733
734 <?php
735
736 $user = get_option('mc_username');
737 $api_key = get_option('mc_apikey');
738
739 // If we have an API Key, see if we need to change the lists and its options
740 if (!empty($api_key)){
741 mailchimpSF_change_list_if_necessary($api_key);
742 }
743
744
745
746 // Display our success/error message(s) if have them
747 if (mailchimpSF_global_msg() != ''){
748 // Message has already been html escaped, so we don't want to 2x escape it here
749 ?>
750 <div id="mc_message" class=""><?php echo mailchimpSF_global_msg(); ?></div>
751 <?php
752 }
753
754
755 // If we don't have an API Key, do a login form
756 if (get_option('mc_apikey') == '') {
757 ?>
758 <div>
759 <form method="post" action="options-general.php?page=mailchimpSF_options">
760 <h3><?php esc_html_e('Login Info', 'mailchimp_i18n');?></h3>
761 <?php esc_html_e('To start using the MailChimp plugin, we first need to login and get your API Key. Please enter your MailChimp API Key below.', 'mailchimp_i18n'); ?>
762
763 <br/>
764
765 <?php
766 echo sprintf(
767 '%1$s <a href="http://www.mailchimp.com/signup/" target="_blank">%2$s</a>',
768 esc_html(__("Don't have a MailChimp account?", 'mailchimp_i18n')),
769 esc_html(__('Try one for Free!', 'mailchimp_i18n'))
770 );
771 ?>
772
773 <br/>
774
775 <table class="form-table">
776 <tr valign="top">
777 <th scope="row"><?php esc_html_e('API Key', 'mailchimp_i18n'); ?>:</th>
778 <td>
779 <input name="mc_apikey" type="text" id="mc_apikey" class="code" value="<?php echo esc_attr($api_key); ?>" size="32" />
780 <br/>
781 <a href="http://admin.mailchimp.com/account/api-key-popup" target="_blank">get your API Key here</a>
782 </td>
783 </tr>
784 </table>
785
786 <input type="hidden" name="mcsf_action" value="update_mc_apikey"/>
787 <input type="submit" name="Submit" value="<?php esc_attr_e('Save & Check', 'mailchimp_i18n');?>" class="button" />
788 <?php wp_nonce_field('update_mc_api_key', '_mcsf_nonce_action'); ?>
789 </form>
790 </div>
791
792 <?php
793 if (get_option('mc_username')!=''){
794 ?>
795 <strong><?php esc_html_e('Notes', 'mailchimp_i18n'); ?>:</strong>
796 <ul>
797 <li><em><?php esc_html_e('Changing your settings at MailChimp.com may cause this to stop working.', 'mailchimp_i18n'); ?></em></li>
798 <li><em><?php esc_html_e('If you change your login to a different account, the info you have setup below will be erased.', 'mailchimp_i18n'); ?></em></li>
799 <li><em><?php esc_html_e('If any of that happens, no biggie - just reconfigure your login and the items below...', 'mailchimp_i18n'); ?></em></li>
800 </ul>
801 <br/>
802 <?php
803 }
804 } // End of login form
805
806 // Start logout form
807 else {
808 ?>
809 <table style="min-width:400px;">
810 <tr>
811 <td><h3><?php esc_html_e('Logged in as', 'mailchimp_i18n');?>: <?php echo esc_html(get_option('mc_username')); ?></h3>
812 </td>
813 <td>
814 <form method="post" action="options-general.php?page=mailchimpSF_options">
815 <input type="hidden" name="mcsf_action" value="logout"/>
816 <input type="submit" name="Submit" value="<?php esc_attr_e('Logout', 'mailchimp_i18n');?>" class="button" />
817 <?php wp_nonce_field('mc_logout', '_mcsf_nonce_action'); ?>
818 </form>
819 </td>
820 </tr>
821 </table>
822 <?php
823 } // End Logout form
824
825
826
827 //Just get out if nothing else matters...
828 if (get_option('mc_apikey') == '') return;
829
830 if (get_option('mc_apikey')!=''){
831 ?>
832 <h3><?php esc_html_e('Your Lists', 'mailchimp_i18n'); ?></h3>
833
834 <div>
835
836 <p><?php esc_html_e('Please select the List you wish to create a Signup Form for.', 'mailchimp_i18n'); ?></p>
837
838 <form method="post" action="options-general.php?page=mailchimpSF_options">
839 <?php
840 $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
841 //we *could* support paging, but few users have that many lists (and shouldn't)
842 $lists = $api->lists(array(),0,100);
843 $lists = $lists['data'];
844
845 if (count($lists) == 0) {
846 ?>
847 <span class='error_msg'>
848 <?php
849 echo sprintf(
850 esc_html(__("Uh-oh, you don't have any lists defined! Please visit %s, login, and setup a list before using this tool!", 'mailchimp_i18n')),
851 "<a href='http://www.mailchimp.com/'>MailChimp</a>"
852 );
853 ?>
854 </span>
855 <?php
856 }
857 else {
858 ?>
859 <table style="min-width:400px">
860 <tr>
861 <td>
862 <select name="mc_list_id" style="min-width:200px;">
863 <option value=""> &mdash; <?php esc_html_e('Select A List','mailchimp_i18n'); ?> &mdash; </option>
864 <?php
865 foreach ($lists as $list) {
866 $option = get_option('mc_list_id');
867 ?>
868 <option value="<?php echo esc_attr($list['id']); ?>"<?php selected($list['id'], $option); ?>><?php echo esc_html($list['name']); ?></option>
869 <?php
870 }
871 ?>
872 </select>
873 </td>
874 <td>
875 <input type="hidden" name="mcsf_action" value="update_mc_list_id" />
876 <input type="submit" name="Submit" value="<?php esc_attr_e('Update List', 'mailchimp_i18n'); ?>" class="button" />
877 </td>
878 </tr>
879 <tr>
880 <td colspan="2">
881 <strong><?php esc_html_e('Note:', 'mailchimp_i18n'); ?></strong> <em><?php esc_html_e('Updating your list will not cause settings below to be lost. Changing to a new list will.', 'mailchimp_i18n'); ?></em>
882 </td>
883 </tr>
884 </table>
885 <?php
886 } //end select list
887 ?>
888 </form>
889 </div>
890
891 <br/>
892
893 <?php
894 }
895 else {
896 //display the selected list...
897 ?>
898
899 <p class="submit">
900 <form method="post" action="options-general.php?page=mailchimpSF_options">
901 <input type="hidden" name="mcsf_action" value="reset_list" />
902 <input type="submit" name="reset_list" value="<?php esc_attr_e('Reset List Options and Select again', 'mailchimp_i18n'); ?>" class="button" />
903 <?php wp_nonce_field('reset_mailchimp_list', '_mcsf_nonce_action'); ?>
904 </form>
905 </p>
906 <h3><?php esc_html_e('Subscribe Form Widget Settings for this List', 'mailchimp_i18n'); ?>:</h3>
907 <h4><?php esc_html_e('Selected MailChimp List', 'mailchimp_i18n'); ?>: <?php echo esc_html(get_option('mc_list_name')); ?></h4>
908 <?php
909 }
910 //Just get out if nothing else matters...
911 if (get_option('mc_list_id') == '') return;
912
913
914 // The main Settings form
915 ?>
916
917 <div>
918 <form method="post" action="options-general.php?page=mailchimpSF_options">
919 <div style="width:600px;">
920 <input type="hidden" name="mcsf_action" value="change_form_settings">
921 <?php wp_nonce_field('update_general_form_settings', '_mcsf_nonce_action'); ?>
922 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />
923 <table class="widefat">
924 <tr valign="top">
925 <th scope="row"><?php esc_html_e('Monkey Rewards', 'mailchimp_i18n'); ?>:</th>
926 <td><input name="mc_rewards" type="checkbox"<?php if (get_option('mc_rewards')=='on' || get_option('mc_rewards')=='' ) { echo ' checked="checked"'; } ?> id="mc_rewards" class="code" />
927 <em><label for="mc_rewards"><?php esc_html_e('turning this on will place a "powered by MailChimp" link in your form that will earn you credits with us. It is optional and can be turned on or off at any time.', 'mailchimp_i18n'); ?></label></em>
928 </td>
929 </tr>
930 <tr valign="top">
931 <th scope="row"><?php esc_html_e('Use Javascript Support?', 'mailchimp_i18n'); ?>:</th>
932 <td><input name="mc_use_javascript" type="checkbox" <?php checked(get_option('mc_use_javascript'), 'on'); ?> id="mc_use_javascript" class="code" />
933 <em><label for="mc_use_javascript"><?php esc_html_e('turning this on will use fancy javascript submission and should degrade gracefully for users not using javascript. It is optional and can be turned on or off at any time.', 'mailchimp_i18n'); ?></label></em>
934 </td>
935 </tr>
936 <tr valign="top">
937 <th scope="row"><?php esc_html_e('Use Javascript Datepicker?', 'mailchimp_i18n'); ?>:</th>
938 <td><input name="mc_use_datepicker" type="checkbox" <?php checked(get_option('mc_use_datepicker'), 'on'); ?> id="mc_use_datepicker" class="code" />
939 <em><label for="mc_use_datepicker"><?php esc_html_e('turning this on will use the jQuery UI Datepicker for dates.', 'mailchimp_i18n'); ?></label></em>
940 </td>
941 </tr>
942 <tr valign="top">
943 <th scope="row"><?php esc_html_e('Include Unsubscribe link?', 'mailchimp_i18n'); ?>:</th>
944 <td><input name="mc_use_unsub_link" type="checkbox"<?php checked(get_option('mc_use_unsub_link'), 'on'); ?> id="mc_use_unsub_link" class="code" />
945 <em><label for="mc_use_unsub_link"><?php esc_html_e('turning this on will add a link to your host unsubscribe form', 'mailchimp_i18n'); ?></label></em>
946 </td>
947 </tr>
948 <tr valign="top">
949 <th scope="row"><?php esc_html_e('Header content', 'mailchimp_i18n'); ?>:</th>
950 <td>
951 <textarea name="mc_header_content" rows="2" cols="50"><?php echo esc_html(get_option('mc_header_content')); ?></textarea><br/>
952 <em><?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?></em>
953 </td>
954 </tr>
955
956 <tr valign="top">
957 <th scope="row"><?php esc_html_e('Sub-header content', 'mailchimp_i18n'); ?>:</th>
958 <td>
959 <textarea name="mc_subheader_content" rows="2" cols="50"><?php echo esc_html(get_option('mc_subheader_content')); ?></textarea><br/>
960 <em><?php esc_html_e('You can fill this with your own Text, HTML markup (including image links), or Nothing!', 'mailchimp_i18n'); ?></em>.
961 <?php esc_html_e('This will be displayed under the heading and above the form.', 'mailchimp_i18n'); ?>
962 </td>
963 </tr>
964
965
966 <tr valign="top">
967 <th scope="row"><?php esc_html_e('Submit Button text', 'mailchimp_i18n'); ?>:</th>
968 <td>
969 <input type="text" name="mc_submit_text" size="30" value="<?php echo esc_attr(get_option('mc_submit_text')); ?>"/>
970 </td>
971 </tr>
972
973 <tr valign="top">
974 <th scope="row"><?php esc_html_e('Custom Styling', 'mailchimp_i18n'); ?>:</th>
975 <td>
976 <table class="widefat">
977
978 <tr><th><label for="mc_custom_style"><?php esc_html_e('Turned On?', 'mailchimp_i18n'); ?></label></th><td><input type="checkbox" name="mc_custom_style" id="mc_custom_style"<?php checked(get_option('mc_custom_style'), 'on'); ?> /></td></tr>
979 <tr><th colspan="2"><?php esc_html_e('Header Settings (only applies if there are no HTML tags in the Header Content area above)', 'mailchimp_i18n'); ?>:</th></tr>
980 <tr><th><?php esc_html_e('Border Width', 'mailchimp_i18n'); ?>:</th><td><input type="text" name="mc_header_border_width" size="3" maxlength="3" value="<?php echo esc_attr(get_option('mc_header_border_width')); ?>"/> px<br/>
981 <em><?php esc_html_e('Set to 0 for no border, do not enter', 'mailchimp_i18n'); ?> <strong>px</strong>!</em>
982 </td></tr>
983 <tr><th><?php esc_html_e('Border Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_header_border_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_header_border_color')); ?>"/><br/>
984 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
985 </td></tr>
986 <tr><th><?php esc_html_e('Text Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_header_text_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_header_text_color')); ?>"/><br/>
987 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
988 </td></tr>
989 <tr><th><?php esc_html_e('Background Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_header_background" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_header_background')); ?>"/><br/>
990 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
991 </td></tr>
992
993 <tr><th colspan="2"><?php esc_html_e('Form Settings', 'mailchimp_i18n'); ?>:</th></tr>
994 <tr><th><?php esc_html_e('Border Width', 'mailchimp_i18n'); ?>:</th><td><input type="text" name="mc_form_border_width" size="3" maxlength="3" value="<?php echo esc_attr(get_option('mc_form_border_width')); ?>"/> px<br/>
995 <em><?php esc_html_e('Set to 0 for no border, do not enter', 'mailchimp_i18n'); ?> <strong>px</strong>!</em>
996 </td></tr>
997 <tr><th><?php esc_html_e('Border Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_form_border_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_border_color')); ?>"/><br/>
998 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
999 </td></tr>
1000 <tr><th><?php esc_html_e('Text Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_form_text_color" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_text_color')); ?>"/><br/>
1001 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
1002 </td></tr>
1003 <tr><th><?php esc_html_e('Background Color', 'mailchimp_i18n'); ?>:</th><td>#<input type="text" name="mc_form_background" size="7" maxlength="6" value="<?php echo esc_attr(get_option('mc_form_background')); ?>"/><br/>
1004 <em><?php esc_html_e('do not enter initial', 'mailchimp_i18n'); ?> <strong>#</strong></em>
1005 </td></tr>
1006 </table>
1007 </td>
1008 </tr>
1009 </table>
1010 </div>
1011 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />
1012
1013 <?php
1014 // Merge Variables Table
1015 ?>
1016 <div style="width:400px;">
1017
1018 <h4><?php esc_html_e('Merge Variables Included', 'mailchimp_i18n'); ?></h4>
1019
1020 <?php
1021 $mv = get_option('mc_merge_vars');
1022
1023 if (count($mv) == 0 || !is_array($mv)){
1024 ?>
1025 <em><?php esc_html_e('No Merge Variables found.', 'mailchimp_i18n'); ?></em>
1026 <?php
1027 } else {
1028 ?>
1029
1030 <table class='widefat'>
1031 <tr valign="top">
1032 <th><?php esc_html_e('Name', 'mailchimp_i18n');?></th>
1033 <th><?php esc_html_e('Tag', 'mailchimp_i18n');?></th>
1034 <th><?php esc_html_e('Required?', 'mailchimp_i18n');?></th>
1035 <th><?php esc_html_e('Include?', 'mailchimp_i18n');?></th>
1036 </tr>
1037 <?php
1038 foreach($mv as $var){
1039 ?>
1040 <tr valign="top">
1041 <td><?php echo esc_html($var['name']); ?></td>
1042 <td><?php echo esc_html($var['tag']); ?></td>
1043 <td><?php echo esc_html(($var['req'] == 1) ? 'Y' : 'N'); ?></td>
1044 <td>
1045 <?php
1046 if (!$var['req']){
1047 $opt = 'mc_mv_'.$var['tag'];
1048 ?>
1049 <input name="<?php echo esc_attr($opt); ?>" type="checkbox" id="<?php echo esc_attr($opt); ?>" class="code"<?php checked(get_option($opt), 'on'); ?> />
1050 <?php
1051 } else {
1052 ?>
1053 &nbsp;&mdash;&nbsp;
1054 <?php
1055 }
1056 ?>
1057 </td>
1058 </tr>
1059 <?php
1060 }
1061 ?>
1062 </table>
1063 <?php
1064 }
1065
1066 ?>
1067
1068 <h4><?php esc_html_e('Interest Groups', 'mailchimp_i18n'); ?></h4>
1069
1070 <?php
1071 // Interest Groups Table
1072 $igs = get_option('mc_interest_groups');
1073 if (is_array($igs) && !isset($igs['id'])) {
1074 // Determines whether or not to continue processing. Only false if there was an error.
1075 $continue = true;
1076 foreach ($igs as $ig) {
1077 if ($continue) {
1078 if (!is_array($ig) || empty($ig) || $ig == 'N' ) {
1079 ?>
1080 <em><?php esc_html_e('No Interest Groups Setup for this List', 'mailchimp_i18n'); ?></em>
1081 <?php
1082 $continue = false;
1083 }
1084 else {
1085 ?>
1086 <table class='widefat'>
1087 <tr valign="top">
1088 <th width="75px">
1089 <label for="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>"><?php esc_html_e('Show?', 'mailchimp_i18n'); ?></label>
1090 </th>
1091 <th>
1092 <input name="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>" id="<?php echo esc_attr('mc_show_interest_groups_'.$ig['id']); ?>" type="checkbox" class="code"<?php checked('on', get_option('mc_show_interest_groups_'.$ig['id'])); ?> />
1093 </th>
1094 </tr>
1095 <tr valign="top">
1096 <th><?php esc_html_e('Name', 'mailchimp_i18n'); ?>:</th>
1097 <th><?php echo esc_html($ig['name']); ?></th>
1098 </tr>
1099 <tr valign="top">
1100 <th><?php esc_html_e('Input Type', 'mailchimp_i18n'); ?>:</th>
1101 <td><?php echo esc_html($ig['form_field']); ?></td>
1102 </tr>
1103 <tr valign="top">
1104 <th><?php esc_html_e('Options', 'mailchimp_i18n'); ?>:</th>
1105 <td>
1106 <ul>
1107 <?php
1108 foreach($ig['groups'] as $interest){
1109 ?>
1110 <li><?php echo esc_html($interest['name']); ?></li>
1111 <?php
1112 }
1113 ?>
1114 </ul>
1115 </td>
1116 </tr>
1117 </table>
1118 <?php
1119 }
1120 }
1121 }
1122 }
1123 else {
1124 ?>
1125 <em><?php esc_html_e('Error retrieving interest groups. Please re-import the list.', 'mailchimp_i18n'); ?></em>
1126 <?php
1127 }
1128
1129 ?>
1130 <p class="submit">
1131 <input type="submit" value="<?php esc_attr_e('Update Subscribe Form Settings', 'mailchimp_i18n'); ?>" class="button" />
1132 </p>
1133 </div>
1134 </form>
1135 </div>
1136 </div><!--wrap-->
1137 <?php
1138 }//mailchimpSF_setup_page()
1139
1140
1141 function mailchimpSF_register_widgets() {
1142 register_widget('mailchimpSF_Widget');
1143 }
1144 add_action('widgets_init', 'mailchimpSF_register_widgets');
1145
1146 function mailchimpSF_shortcode($atts){
1147 ob_start();
1148 mailchimpSF_signup_form();
1149 return ob_get_clean();
1150 }
1151 add_shortcode('mailchimpsf_form', 'mailchimpSF_shortcode');
1152
1153 /**
1154 * Attempts to signup a user, per the $_POST args.
1155 *
1156 * This sets a global message, that is then used in the widget
1157 * output to retrieve and display that message.
1158 *
1159 * @return bool
1160 */
1161 function mailchimpSF_signup_submit() {
1162 $mv = get_option('mc_merge_vars', array());
1163 $mv_tag_keys = array();
1164
1165 $igs = get_option('mc_interest_groups', array());
1166
1167 $success = true;
1168 $listId = get_option('mc_list_id');
1169 $email = isset($_POST['mc_mv_EMAIL']) ? strip_tags(stripslashes($_POST['mc_mv_EMAIL'])) : '';
1170 $merge = $errs = array(); // Set up some vars
1171
1172 // Loop through our Merge Vars, and if they're empty, but required, then print an error, and mark as failed
1173 foreach($mv as $var) {
1174 $opt = 'mc_mv_'.$var['tag'];
1175
1176 $opt_val = isset($_POST[$opt]) ? $_POST[$opt] : '';
1177
1178 if (is_array($opt_val)) {
1179 $opt_val = implode('', $opt_val);
1180 }
1181
1182 if ($var['req'] == 'Y' && trim($opt_val) == '') {
1183 $success = false;
1184 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
1185 }
1186 else {
1187 if ($var['tag'] != 'EMAIL') {
1188 $merge[$var['tag']] = $opt_val;
1189 }
1190 }
1191
1192 // We also want to create an array where the keys are the tags for easier validation later
1193 $mv_tag_keys[$var['tag']] = $var;
1194
1195 }
1196
1197 // Head back to the beginning of the merge vars array
1198 reset($mv);
1199
1200 // Ensure we have an array
1201 $igs = !is_array($igs) ? array() : $igs;
1202
1203 foreach ($igs as $ig) {
1204 if (get_option('mc_show_interest_groups_'.$ig['id']) == 'on') {
1205 $groupings = array();
1206 switch ($ig['form_field']) {
1207 case 'select':
1208 case 'dropdown':
1209 case 'radio':
1210 if (isset($_POST['group'][$ig['id']])) {
1211 $groupings = array(
1212 'id' => $ig['id'],
1213 'groups' => str_replace(',', '\,', $_POST['group'][$ig['id']]),
1214 );
1215 }
1216 break;
1217 case 'checkboxes':
1218 case 'checkbox':
1219 if (isset($_POST['group'][$ig['id']])) {
1220 foreach ($_POST['group'][$ig['id']] as $i => $value) {
1221 $groups .= str_replace(',', '\,', $value).',';
1222
1223 }
1224 $groupings = array(
1225 'id' => $ig['id'],
1226 'groups' => $groups,
1227 );
1228 }
1229 break;
1230 default:
1231 // Nothing
1232 break;
1233 }
1234 if (!isset($merge['GROUPINGS']) || !is_array($merge['GROUPINGS'])) {
1235 $merge['GROUPINGS'] = array();
1236 }
1237 if (!empty($groupings)) {
1238 $merge['GROUPINGS'][] = $groupings;
1239 }
1240 }
1241 }
1242
1243 // If we're good
1244 if ($success) {
1245 // Clear out empty merge vars
1246
1247 foreach ($merge as $k => $v) {
1248 if (is_array($v) && empty($v)) {
1249 unset($merge[$k]);
1250 }
1251 else if (!is_array($v) && trim($v) === '') {
1252 unset($merge[$k]);
1253 }
1254 }
1255
1256 // If we have an empty $merge, then assign empty string.
1257 if (count($merge) == 0 || $merge == '') {
1258 $merge = '';
1259 }
1260
1261 if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) {
1262 $email_type = $_POST['email_type'];
1263 }
1264 else {
1265 $email_type = 'html';
1266 }
1267
1268 // Custom validation based on type
1269 if (is_array($merge) && !empty($merge)) {
1270 foreach ($merge as $merge_key => $merge_value) {
1271 if ($merge_key !== 'GROUPINGS') {
1272 switch ($mv_tag_keys[$merge_key]['field_type']) {
1273 case 'phone':
1274 $phone = implode($merge_value, '');
1275 if (!empty($phone)) {
1276 if (preg_match('/[^0-9]/', $phone)) {
1277 $errs[] = sprintf(__("%s must consist of only numbers", 'mailchimp_i18n'), esc_html($mv_tag_keys[$merge_key]['name']));
1278 $success = false;
1279 }
1280 }
1281 break;
1282
1283 default:
1284 break;
1285 }
1286 }
1287 }
1288 }
1289 if ($success) {
1290 $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
1291 $retval = $api->listSubscribe( $listId, $email, $merge, $email_type);
1292 if (!$retval) {
1293 switch($api->errorCode) {
1294 case '105' :
1295 $errs[] = __("Please try again later", 'mailchimp_i18n').'.';
1296 break;
1297 case '214' :
1298 $errs[] = __("That email address is already subscribed to the list", 'mailchimp_i18n').'.';
1299 break;
1300 case '250' :
1301 list($field, $rest) = explode(' ', $api->errorMessage, 2);
1302 $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
1303 break;
1304 case '254' :
1305 list($i1, $i2, $i3, $field, $rest) = explode(' ',$api->errorMessage,5);
1306 $errs[] = sprintf(__("%s has invalid content.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
1307 break;
1308 case '270' :
1309 $errs[] = __("An invalid Interest Group was selected", 'mailchimp_i18n').'.';
1310 break;
1311 case '502' :
1312 $errs[] = __("That email address is invalid", 'mailchimp_i18n').'.';
1313 break;
1314 default:
1315 $errs[] = $api->errorCode.":".$api->errorMessage;
1316 break;
1317 }
1318 $success = false;
1319 }
1320 }
1321 }
1322
1323 // If we have errors, then show them
1324 if (count($errs) > 0) {
1325 $msg = '<span class="mc_error_msg">';
1326 foreach($errs as $error){
1327 $msg .= '&raquo; '.esc_html($error).'<br />';
1328 }
1329 $msg .= '</span>';
1330 }
1331 else {
1332 $msg = "<strong class='mc_success_msg'>".esc_html(__("Success, you've been signed up! Please look for our confirmation email!", 'mailchimp_i18n'))."</strong>";
1333 }
1334
1335 // Set our global message
1336 mailchimpSF_global_msg($msg);
1337
1338 return $success;
1339 }
1340
1341
1342
1343 /**********************
1344 * Utility Functions *
1345 **********************/
1346 /**
1347 * Utility function to allow placement of plugin in plugins, mu-plugins, child or parent theme's plugins folders
1348 *
1349 * This function must be ran _very early_ in the load process, as it sets up important constants for the rest of the plugin
1350 */
1351 function mailchimpSF_where_am_i() {
1352 $locations = array(
1353 'plugins' => array(
1354 'dir' => WP_PLUGIN_DIR,
1355 'url' => plugins_url()
1356 ),
1357 'mu_plugins' => array(
1358 'dir' => WPMU_PLUGIN_DIR,
1359 'url' => plugins_url(),
1360 ),
1361 'template' => array(
1362 'dir' => trailingslashit(get_template_directory()).'plugins/',
1363 'url' => trailingslashit(get_template_directory_uri()).'plugins/',
1364 ),
1365 'stylesheet' => array(
1366 'dir' => trailingslashit(get_stylesheet_directory()).'plugins/',
1367 'url' => trailingslashit(get_stylesheet_directory_uri()).'plugins/',
1368 ),
1369 );
1370
1371 // Set defaults
1372 $mscf_dir = trailingslashit(WP_PLUGIN_DIR).'mailchimp/';
1373 $mscf_url = trailingslashit(WP_PLUGIN_URL).'mailchimp/';
1374
1375 // Try our hands at finding the real location
1376 foreach ($locations as $key => $loc) {
1377 $dir = trailingslashit($loc['dir']).'mailchimp/';
1378 $url = trailingslashit($loc['url']).'mailchimp/';
1379 if (is_file($dir.basename(__FILE__))) {
1380 $mscf_dir = $dir;
1381 $mscf_url = $url;
1382 break;
1383 }
1384 }
1385
1386 // Define our complete filesystem path
1387 define('MCSF_DIR', $mscf_dir);
1388
1389 /* Lang location needs to be relative *from* ABSPATH,
1390 so strip it out of our language dir location */
1391 define('MCSF_LANG_DIR', trailingslashit(MCSF_DIR).'po/');
1392
1393 // Define our complete URL to the plugin folder
1394 define('MCSF_URL', $mscf_url);
1395 }
1396
1397
1398 ?>
1399